Map and Reduce Monad for Clojure… What about a Juxt Monad?

后端 未结 3 1877
感动是毒
感动是毒 2021-02-03 13:58

Whilst learning Clojure, I\'ve spent ages trying to make sense of monads - what they are and how we can use them.... with not too much success. However, I found an excellent \'M

相关标签:
3条回答
  • 2021-02-03 14:27

    I found some really good monads resources:

    • http://www.clojure.net/tags.html#monads-ref (Jim Duey's Monads Guide, which really goes into the nitty gritty monad definitions)
    • http://homepages.inf.ed.ac.uk/wadler/topics/monads.html#marktoberdorf (A whole load of papers on monads)
    • http://vimeo.com/17207564 (A talk on category theory, which I half followed)

    So from Jim's Guide - http://www.clojure.net/2012/02/06/Legalities/ - he gives three axioms for definition of 'bind-m' and 'reduce-m' functions:

    Identity The first Monad Law can be written as

    (m-bind (m-result x) f) is equal to (f x)

    What this means is that whatever m-result does to x to make it into a monadic value, m-bind undoes before applying f to x. So with regards to m-bind, m-result is sort of like an identity function. Or in category theory terminology, its unit. Which is why sometimes you’ll see it named ‘unit’.

    Reverse Identity The second Monad Law can be written as

    (m-bind mv m-result) is equal to mv where mv is a monadic value.

    This law is something like a complement to the first law. It basically ensures that m-result is a monadic function and that whatever m-bind does to a monadic value to extract a value, m-result undoes to create a monadic value.

    Associativity The third Monad Law can be written as

    (m-bind (m-bind mv f) g) is equal to (m-bind mv (fn [x] (m-bind (f x) g))) where f and g are monadic functions and mv is a monadic value.

    What this law is saying is that it doesnt matter whether the f is applied to mv and then g is applied to the result, or whether a new monadic function is created out of a composition of f and g which is then applied to mv. In either order, the resulting value is the same monadic value. Stated another way, m-bind is left and right associative.

    In http://www.clojure.net/2012/02/04/Sets-not-lists/ he gives an monad that takes sets as inputs instead of taking lists. Will work through all the examples...

    0 讨论(0)
  • 2021-02-03 14:37

    Your questions are not a type of monads. They seems more like a syntactic sugar and that you can accomplish using macros and I won't even suggest you do that because map, reduce etc are simple functions and there is no need to make their interface complex.

    The reason these cases are not monads because moands are sort of amplified values which wrap normal values. In map and reduce case the vector that you use doesn't needs to be amplified to allow it to work in map or reduce.

    It would be helpful for you to try macroexpand on the domoand expressions. For ex: the sequence monad example should expand to something like:

    (bind (result (range 5)) 
       (fn [a] 
          (bind (result (range a))
            (fn [b] (* a b))
       )))
    

    Where the bind and result are functions defined in the sequence monad m-bind and m-result. So basically the vector expression(s) after the domand get nested and the expression(s) after the vector are used as it is in above case the (* a b) is called as it is (just that the a and b values are provided by the monad). In your example of map monad the vector expressions are supposed to be as it is and the last expression (+ a b) should somehow mean (map + a b) which is not what a monad is supposed to do.

    0 讨论(0)
  • 2021-02-03 14:46

    Your examples are not monads. A monad represents composable computational steps. In the trivial identity monad, the computational step is just an expression evaluation.

    In the maybe monad, a step is an expression that may succeed or fail.

    In the sequence monad, a step is an expression that produces a variable number of results (the elements of the sequence).

    In the writer monad, a computational step is a combination of expression evaluation and log output. In the state monad, a computational step involves accessing and/or modifying a piece of mutable state.

    In all these cases, the monad plumbery takes care of correctly combining steps. The m-result function packages a "plain" value to fit into the monadic computation scheme, and the m-bind function feeds the result of one computational step into the next computational step.

    In (map + a b), there are no computational steps to be combined. There is no notion of order. It's just nested expression evaluation. Same for reduce.

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