For instance, I have an easy code snippet:
import cats.instances.option._
import cats.syntax.option._
import cats.syntax.apply._
val a = 1.some
val b = 2.some
(a, b).mapN(_ + _)
I wish to fold this to result or 0
.
Are there any alternatives to .getOrElse(0)
?
Maybe a kind of:
(a, b).foldN(0)(_ + _) // foldN actually does not exists
Do you have to use a tuple there? If you used
NonEmptyList
orNonEmptyChain
you can simply door
or any other combination utilizes of
NonEmpty*
orTraverse
.You cannot run away from
getOrElse
entirely. At some point you have to provide someOption[Int] => Int
mapping, either infoldMap
,reduceMap
or afterfold
ing/reduce
ing. You might replace that withfold
onOption
or something else but noApplicative
utility would do this for you as exiting the contextF
is not a part of anApplicative
interface (more like aComonad
).