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
NonEmptyListorNonEmptyChainyou can simply door
or any other combination utilizes of
NonEmpty*orTraverse.You cannot run away from
getOrElseentirely. At some point you have to provide someOption[Int] => Intmapping, either infoldMap,reduceMapor afterfolding/reduceing. You might replace that withfoldonOptionor something else but noApplicativeutility would do this for you as exiting the contextFis not a part of anApplicativeinterface (more like aComonad).