简体   繁体   English

Scala相当于Haskell monads

[英]Scala equivalent to Haskell monads

I had some experience in Haskell and currently learning Scala. 我在Haskell有一些经验,目前正在学习Scala。 Am wondering whether there is something equivalent to Monads in Scala?? 我想知道Scala中是否有相当于Monads的东西?

You probably want to check out scalaz ; 你可能想查看scalaz ; it's been strongly influenced by Haskell. 它受到了Haskell的强烈影响。 Indeed, it has often been asked of one of the prime contributors why they aren't just using Haskell, as they seem to like it so much! 事实上,人们常常被问到一个主要的贡献者,他们为什么不只是使用Haskell,因为他们似乎非常喜欢它!

Scalaz makes heavy use of implicits in order to decorate structures with their monads. Scalaz大量使用implicits以使用monad来装饰结构。 For example: 例如:

val fibs = (0, 1).iterate[Stream]( i => i._2 -> (i._2 + i._1) ).map(_._1)
println( fibs.take(10) )

我认为值得注意的是,斯卡拉的“理解”等同于哈斯克尔的monadic“do”

There is no explicit concept of monad in Scala standard library (there is no appropriate trait/class or typeclass). Scala标准库中没有明确的monad概念(没有合适的特征/类或类型类)。

Scala deals with this in kind of an ad-hoc manner, see Scala by example 's section on for comprehensions for details. Scala以一种特殊的方式处理这个问题,请参阅Scala的示例部分以了解详细信息。

Both Option and List are monads. Option和List都是monad。 I also believe that Either's left and right projections are also monads. 我也相信Either的左右投影也是monad。

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

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