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

前端 未结 1 1810
我在风中等你
我在风中等你 2021-02-02 08:28

In the chain documentation you find:

Calling chain on a wrapped object will cause all future method calls to return wrapped objects as well

相关标签:
1条回答
  • 2021-02-02 09:13

    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.

    0 讨论(0)
提交回复
热议问题