Does the chain function in underscore.js create a monad?

白昼怎懂夜的黑 提交于 2019-12-20 10:00:29

问题


In the chain documentation you find:

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.

So does the chain function create a monad?


回答1:


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

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

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

See this Stackoverflow question for more about comonads.



来源:https://stackoverflow.com/questions/10431999/does-the-chain-function-in-underscore-js-create-a-monad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!