What should be used instead of someMethod in the following code to get correct results?
case class case class Employee(name: String, department: String, manager: Option[String])

def lookupByName(name: String): Option[Employee] = name match {
  case "Joe" => Some(Employee("Joe", "Finances", Some("Julie")))
  case "Mary" => Some(Employee("Mary", "IT", None))
  case "Izumi" => Some(Employee("Izumi", "IT", Some("Mary")))
  case _ => None

def getManager(employee: Option[Employee]): Option[String] = employee.flatMap(_.manager)

getManager(lookupByName("Joe")).someMethod(Some("Mr. CEO")) == Some("Julie")
getManager(lookupByName("Mary")).someMethod(Some("Mr. CEO")) == Some("Mr. CEO")
getManager(lookupByName("Foo")).someMethod(Some("Mr. CEO")) == Some("Mr. CEO")
  • orElse returns the original Option if not None, or returns the provided Option as an alternative in that case:
    def orElse[B >: A](ob: => Option[B]): Option[B] = this map (Some(_)) getOrElse ob

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback