Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created January 21, 2013 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/4588672 to your computer and use it in GitHub Desktop.
Save dustingetz/4588672 to your computer and use it in GitHub Desktop.
for comprehensions as monad in clojure
(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])
@dustingetz
Copy link
Author

this is from Jim Duey's unsession at clojure conj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment