I need a function which does something like the code below
def function[A,B](a: Option[A], f: Function[A,B]) = {
a match {
case None => None
case Some(v) => Some(f(v))
}
}
Is there any scala built-in function which does the same?
def function[A,B](a: Option[A], f: Function[A,B]) = {
a.map(f(_))
}
Option can be treated as a Monad so many operations such as map, flatMap, and filter are available on it.
http://www.scala-lang.org/api/current/index.html#scala.Option
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With