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
}

lookupByName("Joe").someMethod(_.department != "IT") == Some(Employee("Joe", "Finances", Some("Julie")))
lookupByName("Mary").someMethod(_.department != "IT") == None
lookupByName("Foo").someMethod(_.department != "IT") == None
Theory
  • filter function that will turn any Option into a None if it doesn't satisfy the provided predicate can be implemented in a following way:
    def filter(f: A => Boolean): Option[A] = this match {
      case Some(a) if f(a) => this
      case _ => None
    }
    

Следи за CodeGalaxy

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

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