除了 Option 之外,标准 Scala monad 是什么?
有时,Option
monad 在 Scala 中几乎变成了一种工具。 Scala 标准库中还有其他 monad 吗?
Option
monad turns pretty much a facility some times in Scala. Are there any other monads in Scala standard library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
路易吉的答案是正确的,但恕我直言,信息量不大。
所有集合都可以实现 monad 接口,但其中
flatMap
的签名不是 monad 的 flatMap。不过,大多数时候它们的行为就像单子一样。 Luigi 列出的几乎所有类都与集合相关。LeftProject
和RightProject
指的是Either
。基本上,Either
不是一个 monad,但是如果你“投影”其中一侧,那么该一侧的行为非常像一个Option
monad。Parser
是一个 monad,它构成了解析器组合器的基础。我承认我不认识
ControlContext
。我想知道它是否与延续(也是单子)有关。Luigi's answer is correct, but not very informative, IMHO.
All collections can implement the monad interface, but the signature for
flatMap
in them is not a monad's flatMap. They'll act like monads most of the time, though. Almost all of the classes listed by Luigi are related to collections.LeftProject
andRightProject
refers toEither
. Basically,Either
is not a monad, but if you "project" one of the sides, then that side acts pretty much like anOption
monad.Parser
is a monad, which forms the basis of parser combinators.I admit I do not recognize
ControlContext
. I wonder if it is related to continuations (which are monads as well).您可以通过在 API 索引。它给出:
You can work this out by looking up the tell-tale
flatMap
in the API index. It gives:以下是 Scalaz 中三个源文件的链接:
看一下实例声明。这可能会让您了解标准库中的哪些类型满足单子接口。
Here are the links to three sources files from Scalaz:
Take a look at instance declarations. That might give you an idea about what types from the standard library satisfy the monadic interface.