I was wondering if scala Either
is really a Monad
in Category Theory sense?. I know that Monads should have bind
and return
methods. What is Either
's bind
then?
Yes, it really is - otherwise it would be in scalaz-outlaws. Either
's bind
is defined something like:
trait Either[A, B] {
def bind[C](f: B => Either[A, C]) = this match {
case Right(b) => f(b)
case Left(a) => Left(a)
}
}
(in practice it's defined via a typeclass, but the above definition would work)
I guess it's more proper to say that for a fixed A
, the type ({type L[B]=Either[A, B]})#L
forms a Monad
, so Either
is more a class of Monads
than a Monad
in its own right, but that's an exceedingly technical distinction.
But it really is a Monad
; it satisfies all the monad laws.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With