简体   繁体   English

Scala是否真的是Monad

[英]Is Scala Either really a Monad

I was wondering if scala Either is really a Monad in Category Theory sense?. 我想知道scala Either是否真的是类别理论意义上的Monad I know that Monads should have bind and return methods. 我知道Monads应该有bindreturn方法。 What is Either 's bind then? 什么是Eitherbind呢?

Yes, it really is - otherwise it would be in scalaz-outlaws. 是的,它确实是 - 否则就会出现在scalaz-outlaws中。 Either 's bind is defined something like: Eitherbind定义如下:

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. 我想更合适的是,对于固定的A ,类型({type L[B]=Either[A, B]})#L形成一个Monad ,所以Either更多的是Monads类而不是Monad类自己的权利,但这是一个非常技术性的区别。

But it really is a Monad ; 但它确实是一个Monad ; it satisfies all the monad laws. 它满足所有monad法则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM