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

Environment

irrelevant

Activity

Show:

Konrad HinsenFebruary 5, 2014 at 11:52 AM

Completed

Details

Assignee

Reporter

Priority

Created September 9, 2012 at 2:47 AM
Updated February 5, 2014 at 11:52 AM
Resolved February 5, 2014 at 11:52 AM

Flag notifications