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

后端 未结 3 1880
感动是毒
感动是毒 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...

提交回复
热议问题