Monday, August 3, 2009

Scala version of F# orElse getOrElse functions post

Just a quick comparison of F# Option orElse getOrElse functions in Scala. Given orElse and getOrElse are already defined in Scala, the equivalent code is:

def f(s:List[(Int, Int)]) = {
def tf(g:Int => Boolean) = s.find {case (_,x) => g(x)}
tf(_ < 0) orElse tf(_ == 0) map(_._1) getOrElse 10

The argument to orElse is defined as lazy in the function declaration via : => Option[B] rather than :Option[B]. However in the F# version, the actual argument type is changed from 'a option to 'a option Lazy.

No comments: