Created
August 4, 2015 12:13
-
-
Save iamthiago/0bc505a79b08b6dec03d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.UUID | |
| import scala.annotation.tailrec | |
| /** | |
| * Created by Thiago Pereira on 8/4/15. | |
| */ | |
| case class Employee(id: UUID, name: String, parent: Option[Employee]) | |
| object TailRecTest { | |
| def main(args: Array[String]) { | |
| val president = Employee(UUID.randomUUID(), "PRESIDENT", None) | |
| val director = Employee(UUID.randomUUID(), "DIRECTOR", Some(president)) | |
| val manager = Employee(UUID.randomUUID(), "MANAGER", Some(director)) | |
| val employee = Employee(UUID.randomUUID(), "EMPLOYEE", Some(manager)) | |
| getParents(employee).foreach(println) | |
| } | |
| def getParents(employee: Employee): List[Employee] = { | |
| @tailrec | |
| def recursion(employee: Employee, employees: List[Employee]): List[Employee] = { | |
| employee.parent match { | |
| case None => employees | |
| case Some(parent) => | |
| recursion(parent, parent :: employees) | |
| } | |
| } | |
| recursion(employee, Nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment