by yummyfajitas · 12y ago · 0 comments
optionalF.map( _.whatever ).getOrElse(fallback)
or perhaps (optionalF <+> optionalFallback).getOrElse(finalFallback)
For those unfamiliar with <+>, see here: http://www.chrisstucchio.com/blog/2014/handle_failure_with_p... optional match {
case Some(x) => whatever(x)
case None => fallback
}
Second, I find the pattern matching code much clearer. Sure, it's also longer, but clarity trumps everything.The speed benefit is well known, as is the corollary, premature optimisation. I expect checking for null is even faster if speed is the main concern. It's an engineering tradeoff, just like making the abstraction to a monad (or monad plus). In the normal course of events I'm more concerned about flexibility than performance and would prefer the abstraction.