Is Scala Either really a Monad

1.8k views Asked by At

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?

1

There are 1 answers

4
lmm On BEST ANSWER

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.