Working on an example from Functional Programming in Scala, I'm working on Option#map2
:
override def map2[A, B, C](fa: Option[A],fb: Option[B])(f: (A, B) => C): Option[C] = {
(fa, fb) match {
case (None, _) => None
case (_, None) => None
case (_, _) => Some(f(fa.get, fb.get)) // runtime-safe get calls
}
}
Is the above implementation reasonable? I presumed that, if either fa
or fb
were None
, then so should be the returned value. Pattern matching seemed the most clean to me, but perhaps there's a cleaner or more concise way to write this method?