Reputation: 555
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?
Upvotes: 3
Views: 68
Reputation: 10428
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
Upvotes: 4