aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2011-02-02 16:27:58 -0800
committerDavid Barksdale <amatus.amongus@gmail.com>2011-05-23 23:16:51 -0500
commit566fa3de25e5938cf4875b9dfd3127f9caa8a81b (patch)
tree0b69e4f77a9cf66d0b5e44ac960c851d93f42f06
parentd4d53ecd0732d4808fe6cc604515f07aca551776 (diff)
The state monad's fetch-val and update-val now accept a zeroamatus
argument in case the state does not contain the given key. Just as before, the zero defaults to nil.
-rw-r--r--modules/monads/src/main/clojure/clojure/contrib/monads.clj23
1 files changed, 14 insertions, 9 deletions
diff --git a/modules/monads/src/main/clojure/clojure/contrib/monads.clj b/modules/monads/src/main/clojure/clojure/contrib/monads.clj
index d164f7eb..a268e778 100644
--- a/modules/monads/src/main/clojure/clojure/contrib/monads.clj
+++ b/modules/monads/src/main/clojure/clojure/contrib/monads.clj
@@ -367,20 +367,25 @@
(defn fetch-val
"Return a state-monad function that assumes the state to be a map and
returns the value corresponding to the given key. The state is not modified."
- [key]
- (domonad state-m
- [s (fetch-state)]
- (key s)))
+ ([key]
+ (fetch-val key nil))
+ ([key zero]
+ (domonad state-m
+ [s (fetch-state)]
+ (get s key zero))))
(defn update-val
"Return a state-monad function that assumes the state to be a map and
replaces the value associated with the given key by the return value
of f applied to the old value. The old value is returned."
- [key f]
- (fn [s]
- (let [old-val (get s key)
- new-s (assoc s key (f old-val))]
- [old-val new-s])))
+ ([key f]
+ (update-val key nil f))
+ ([key zero f]
+ (fn [s]
+ (let [old-val (get s key zero)
+ new-s (assoc s key (f old-val))]
+ [old-val new-s]))))
+
(defn set-val
"Return a state-monad function that assumes the state to be a map and