Scala Functor和Monad的区别 [英] Scala Functor and Monad differences

查看:391
本文介绍了Scala Functor和Monad的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

Scala本身并没有强调在Scala环境中Functor和Monad之间的区别吗? Functor Monad 这些术语。我猜想使用 map 是仿函数的一面,使用 flatMap 是Monad的一面。



对于我来说,寻找并使用 scalaz 是迄今为止获得对scala上下文中的这些功能概念的感觉(与haskell上下文相比)。两年前,当我开始scala时,scalaz代码对我来说很乱,几个月前我再次开始寻找,并且我意识到这真的是这种特定风格的函数式编程的干净实现。



例如 Monad 实现显示monad是指向 functor ,因为它扩展了 Pointed 特征(以及 Applicative trait)。我邀请你去看代码。它具有源代码本身的链接,并且很容易遵循链接。



所以函子更一般。 Monads提供附加功能。要了解当你有函数时或者当你有单子时你可以做什么,你可以看看 MA

我们将看到需要隐式函子的实用方法(特别是应用函子),比如 sequence 以及需要完整monad的某些方法,比如 replicateM

Can please someone explain the differences between Functor and Monad in the Scala context?

解决方案

Scala itself really does not emphasize the Functor and Monad terms that much. I guess using map is the functor side, using flatMap is the Monad side.

For me looking and playing around with scalaz has been so far the best avenue to get a sense of those functional concepts in the scala context (versus the haskell context). Two years ago when I started scala, the scalaz code was gibberish to me, then a few months ago I started looking again and I realized that it's really a clean implementation of that particular style of functional programming.

For instance the Monad implementation shows that a monad is a pointed functor because it extends the Pointed trait (as well as the Applicative trait). I invite you to go look at the code. It has linking in the source itself and it's really easy to follow the links.

So functors are more general. Monads provide additional features. To get a sense of what you can do when you have a functor or when you have a monad, you can look at MA

You'll see utility methods that need an implicit functor (in particular applicative functors) such as sequence and sometime methods that needs a full monad such as replicateM.

这篇关于Scala Functor和Monad的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆