Created
March 21, 2011 15:58
-
-
Save marick/879674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
clojure logger monad by "Briay Marick" demonstrated in vimeo video http://vimeo.com/21307543