Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Scala Either really a Monad

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?

like image 362
goral Avatar asked Nov 24 '14 09:11

goral


1 Answers

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.

like image 68
lmm Avatar answered Sep 27 '22 00:09

lmm