What should be used instead of someMethod in the following code to get correct results?
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")
Theory
  • 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
    

Thanks, @michalrobasz79 . Fixed :)

2019 Oct 19, 1:11:29 AM

Would be nice to fix the syntax highlighting

2019 Oct 18, 2:27:09 PM

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Scala
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты