Using scala for reference, we see a fallback behavior (orElse
) in several places such as PartialFunction
, Option
, and cats EitherOps
.
This feels similar to but not the same as the flattening behavior of a monad. Is there a functional programming term for things that exhibit this behavior?
Edit: Some good answers so far, digging around more in cats, I've found
Semigroup[Option[String]].combine(None, Some("b"))
res0: Option[String] = Some(b)
Semigroup[Option[String]].combine(Some("a"), Some("b"))
res1: Option[String] = Some(ab)
SemigroupK[Option].combineK(None, Some("b"))
res2: Option[String] = Some(b)
SemigroupK[Option].combineK(Some("a"), Some("b"))
res3: Option[String] = Some(a)
SemigroupK[List].combineK(List("a"), List("b"))
res4: List[String] = List(a, b)
Alternative[List].unite(List(None, Some("a"), Some("b")))
res4: List[String] = List(a, b)
So I see now that the scalaz Alt
and haskell Alternative
are not quite the same as the cats Alternative
. The more interesting thing is the SemigroupK
(called Plus
in scalaz according to the cats documentation).
So could we say this behavior is exhibited by a type for which you cannot define a semigroup without also having a semigroup for its inner type (because then we might say the scalaz Alt
and haskell Alternative
are semigroups for such kinds)?