Note that there are some explanatory texts on larger screens.

plurals
  1. POMap and Reduce Monad for Clojure... What about a Juxt Monad?
    text
    copied!<p>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 'Monads for Dummies' Video Series - <a href="http://vimeo.com/20717301" rel="nofollow">http://vimeo.com/20717301</a> - by Brian Marik for Clojure</p> <p>So far, my understanding of monads is that it is sort of like a macro in that it allows a set of statements to be written in a form that is easy to read - but monads are much more formalised. My observations are limited to two examples:</p> <p><strong>1. The Identity Monad (or the 'let' monad)</strong> taken from <a href="http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/" rel="nofollow">http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/</a></p> <p>The form that we wish to write is:</p> <pre><code>(let [a 1 b (inc a)] (* a b)) </code></pre> <p>and the corresponding monad is</p> <pre><code>(domonad identity-m [a 1 b (inc a)] (* a b)) </code></pre> <p><strong>2. The Sequence Monad (or the 'for' monad)</strong> taken from <a href="http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/" rel="nofollow">http://onclojure.com/2009/03/06/a-monad-tutorial-for-clojure-programmers-part-2/</a></p> <p>The form we wish to write is:</p> <pre><code>(for [a (range 5) b (range a)] (* a b)) </code></pre> <p>and the corresponding monad is</p> <pre><code>(domonad sequence-m [a (range 5) b (range a)] (* a b)) </code></pre> <p><strong>Monad Definitions in Clojure</strong></p> <p>Looking at the source, using clojure monads library - <a href="https://github.com/clojure/algo.monads" rel="nofollow">https://github.com/clojure/algo.monads</a>:</p> <pre><code>user=&gt;(use 'clojure.algo.monads) nil </code></pre> <p>indentity monad:</p> <pre><code>user=&gt; (source identity-m) (defmonad identity-m [m-result identity m-bind (fn m-result-id [mv f] (f mv)) ]) </code></pre> <p>sequence monad:</p> <pre><code>user=&gt; (source sequence-m) (defmonad sequence-m [m-result (fn m-result-sequence [v] (list v)) m-bind (fn m-bind-sequence [mv f] (flatten* (map f mv))) m-zero (list) m-plus (fn m-plus-sequence [&amp; mvs] (flatten* mvs)) ]) </code></pre> <p>So my conclusion is that a monad is some sort of a generalised higher-order function that takes in an input-function and input-values, adds its own control logic and spits out a 'thing' that can be used in a 'domonad' block.</p> <p><strong>Question 1</strong></p> <p>So finally, to the questions: I want to learn how to write a monad and say I want to write a 'map monad' that imitates the 'map' form in clojure:</p> <pre><code>(domonad map-m [a [1 2 3 4 5] b [5 6 7 8 9]] (+ a b)) </code></pre> <p>Should be equivalent to</p> <pre><code>(map + [1 2 3 4 5] [5 6 7 8 9]) </code></pre> <p>and return the values</p> <pre><code>[6 8 10 12 14] </code></pre> <p>If I look at the source, it should give me something similar to identity-m and sequence-m:</p> <pre><code>user=&gt; (source map-m) (defmonad map-m [m-result ... m-bind ... m-zero ... m-plus ... ]) </code></pre> <p><strong>Question 2</strong></p> <p>I also want to be able to define 'reduce-m' such that I can write:</p> <pre><code>(domonad reduce-m [a [1 2 3 4 5]] (* a)) </code></pre> <p>this could potentially give me 1 x 2 x 3 x 4 x 5 = 120 or</p> <pre><code>(domonad reduce-m [a [1 2 3 4 5] b [1 2 3 4 5]] (+ a b)) </code></pre> <p>will give me (1+2+3+4+5) + (1+2+3+4+5) = 30</p> <p><strong>Finally</strong> Would I also be able to write a 'juxt monad' that imitates the juxt function but instead of passing in values for binding, I pass in a set of functions. :</p> <pre><code>(domonad juxt-m [a #(+ % 1) b #(* % 2)] '([1 2 3 4 5] b a) ) </code></pre> <p>gives</p> <pre><code>[ [2 2] [4 3] [6 4] [8 5] [9 6] ] </code></pre> <p>Potentially, I could do all of those things with macros so I don't really know how useful these 'monads' will be or if they are even considered 'monads'... With all the resources on the internet, It seems to me that if I wanted to learn Monads properly, I have to learn Haskell and right now, learning another syntactic form is just too hard. I think I found some links that maybe relevant but it is too cryptic for me</p> <p>Please can someone shed some light!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload