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
Зареєструйся Зараз
або Підпишись на майбутні тести