Is there a predefined function x in Scala that combine 2 Options so that
Some(a) x None => Some(a)
None x Some(b) => Some(b)
None x None => None
Is there a predefined function x in Scala that combine 2 Options so that
Some(a) x None => Some(a)
None x Some(b) => Some(b)
None x None => None
It's not hard to do it by hand:
Yes, this is the
orElse
method. It chooses the first defined value, orNone
if neither is defined.In the question comments, you mention you can't have
Some(a)
andSome(b)
, so what you really have isOption[Either[Int,Int]]
. In that case, you can usex.map(_.merge)
to get back toOption[Int]
, eg