algo.monad state-m fetch-val bug and efficiency issue
Description
;; the bug
(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))) ;; does not work for integer or string keys
;; I propose replacing it with (get s key)
;; the efficiency issue : ;; ;; domonad with monad parameter binds all the monad functions, ;; looking these up in the state-m map on each call ;; ;; solution :
(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] (fn [s] [(get s key) s]))
;; - we avoid the monad map lookups ;; - coding style brought up to par with the rest of state-m functions
;; the bug
(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))) ;; does not work for integer or string keys
;; I propose replacing it with (get s key)
;; the efficiency issue :
;;
;; domonad with monad parameter binds all the monad functions,
;; looking these up in the state-m map on each call
;;
;; solution :
(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]
(fn [s]
[(get s key) s]))
;; - we avoid the monad map lookups
;; - coding style brought up to par with the rest of state-m functions