Decreasing parameters in scala?

What is the best way to do this:

def reduce[A](x: Option[A], y: Option[A])(f: (A, A) => A): Option[A] = (x, y) match { case (Some(a), Some(b)) => Some(f(a, b)) case (None, None) => None case (_, None) => x case (None, _) => y } 

I reviewed this question, but its case is slightly different from mine ...

+6
source share
1 answer

The shortest probably

 (x ++ y).reduceLeftOption(f) 

which works because of an implicit conversion from Option to Iterable , which has a method that does exactly what you need.

+14
source

All Articles