Is there a Scala Or really a Monad

I was wondering if scala Either

a is valid Monad

in the sense of theory theory ?. I know Monads should have methods bind

and return

. What is Either

bind

then?

+3


source to share


1 answer


Yes, it really is - otherwise it would be in a skalase-outlaw. Either

bind

is defined as follows:

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 is defined with a typeclass, but the above definition will work)



I suppose it's more correct to say that for a fixed A

type ({type L[B]=Either[A, B]})#L

forms a Monad

, so it Either

is more of a class Monads

than a Monad

in its own right, but this is an extremely technical distinction.

But it really is Monad

; it satisfies all the laws of the monad.

+3


source







All Articles