Skip to content

Instantly share code, notes, and snippets.

@marick
Created March 21, 2011 15:58
Show Gist options
  • Save marick/879674 to your computer and use it in GitHub Desktop.
Save marick/879674 to your computer and use it in GitHub Desktop.
(use 'clojure.contrib.monads)
(defn new-pair [value log] {:value value :log log})
(defn starting-pair [value] (new-pair value nil))
(defn log [base-value]
(fn [log]
(new-pair base-value (cons base-value log))))
(defmonad logging-m
"A monad that logs"
[m-result (fn [base-value]
(fn [log] (new-pair base-value log)))
m-bind (fn [step-fn rest-fn]
(fn [log]
(let [{step-value :value, updated-log :log} (step-fn log)]
( (rest-fn step-value) updated-log))))])
(def steppy (domonad logging-m
[a (m-result 5)
b (log (inc a))
c (log (+ b a))]
(* c b a)))
(steppy [])
@sunilnandihalli
Copy link

clojure logger monad by "Briay Marick" demonstrated in vimeo video http://vimeo.com/21307543

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