underscore.js中的链函数是否创建了monad? [英] Does the chain function in underscore.js create a monad?

查看:89
本文介绍了underscore.js中的链函数是否创建了monad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中,您会发现:

In the chain documentation you find:


在包装对象上调用 chain 会导致所有将来的方法调用
以返回包装的对象。完成
计算后,使用检索最终值。

Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well. When you've finished the computation, use value to retrieve the final value.

因此, chain 功能创建monad?

So does the chain function create a monad?

推荐答案

不,不是一个monad,而是一个comonad!它变成一个函数,它接受一个包装对象并将一个正常值返回到一个函数中,该函数将获取并返回一个包装对象。作为一个Haskell类型的签名可以是:

No, not a monad, but a comonad! It turns a function that takes a wrapped object and returns a normal value into a function that both takes and returns a wrapped object. As a Haskell type signature that would be:

(Wrapped a -> b) -> (Wrapped a -> Wrapped b)

值的类型签名是:

The type signature of value is:

Wrapped a -> a

这些正是您需要的一个comonad。第一个函数通常称为 extend ,第二个函数 extract 。

These are precisely what you need for a comonad. The first function is usually called extend and the second extract.

您可以将一个共同作用视为具有一些额外上下文的价值。这当然正是 chain 所做的。

You can think of a comonad as a value with some extra context. And that is of course exactly what chain does.

请参阅这个Stackoverflow问题了解更多关于comonads的信息。

See this Stackoverflow question for more about comonads.

这篇关于underscore.js中的链函数是否创建了monad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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