public
Created

for comprehensions as monad in clojure

  • Download Gist
gistfile1.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(for [x [:a :b :c]
y [1 2 3]]
[x y])
 
 
(defn bind [mv f] (mapcat f mv))
(bind v1 (fn [x]
(bind v2 (fn [y]
(vector [x y])))))
 
(defmacro m-do [result
[sym1 mv1
sym2 mv2]
expression]
`(bind ~mv1 (fn [~sym1]
(bind ~mv2 (fn [~sym2]
(~result ~expression))))))
 
(m-do vector [x [:a :b :c]
y [1 2 3]]
[x y])

this is from Jim Duey's unsession at clojure conj

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.