diff options
author | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-04-21 11:27:20 +0000 |
---|---|---|
committer | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-04-21 11:27:20 +0000 |
commit | 291513179dfb80108175561a8939f3c6bde3423a (patch) | |
tree | 5459a77c6d39154674c0096a8ac82f7823ff98df /src/clojure/contrib/monads | |
parent | ca4501a2c3708433600d648cc8070071c1c0b293 (diff) |
monads: new monad function m-reduce
Diffstat (limited to 'src/clojure/contrib/monads')
-rw-r--r-- | src/clojure/contrib/monads/examples.clj | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/clojure/contrib/monads/examples.clj b/src/clojure/contrib/monads/examples.clj index a216cb39..29a0b539 100644 --- a/src/clojure/contrib/monads/examples.clj +++ b/src/clojure/contrib/monads/examples.clj @@ -8,7 +8,7 @@ (ns clojure.contrib.monads.examples (:use [clojure.contrib.monads - :only (domonad with-monad m-lift m-seq m-when + :only (domonad with-monad m-lift m-seq m-reduce m-when sequence-m maybe-m state-m fetch-state set-state @@ -222,6 +222,13 @@ [sum12 (reduce (m-lift 2 +) (replicate 12 rng))] (- sum12 6.))) +; Such a reduction is often quite useful, so there's m-reduce predefined +; to simplify it: +(def gaussian2 + (domonad state-m + [sum12 (m-reduce + (replicate 12 rng))] + (- sum12 6.))) + ; The statistics should be strictly the same as above, as long as ; we use the same seed: (mean (take 1000 (value-seq gaussian2 1))) @@ -232,7 +239,7 @@ (with-monad state-m (def gaussian3 ((m-lift 1 #(- % 6.)) - (reduce (m-lift 2 +) (replicate 12 rng))))) + (m-reduce + (replicate 12 rng))))) ; Again, the statistics are the same: (mean (take 1000 (value-seq gaussian3 1))) |