Clojure watch function executing, but 'map' as part of function not executing -
(defonce instance (atom {:id nil :canvas nil :subscribers #{"test1" "test2"})) (defn replace-canvas [canvas id] (println "replaced" id "'s canvas")) (defn update-subscribers [k r old-state new-state] (pprint "this function being called") (pprint "i can execute consecutive forms") (map (partial replace-canvas (new-state :canvas)) (new-state :subscribers))) (add-watch instance :update-subs update-subscribers)
when instance atom changes, watch function ostensibly called because "this function being called" , "i can execute consecutive forms" both printed, map function never seems take place. i'm not sure why be, , couldn't find in documentation add-watch placed restrictions on put inside watch function.
even if replace map in update-subscribers more straightforward like
(map println (new-state :subscribers))
the map doen't seem have effect.
any appreciated, thanks!
the map
function lazy work if caller realizes sequence produces (which not case when function invoked part of watch because result thrown away).
you'll either want wrap map
call in doall
, or use doseq
instead (which more idiomatic side-effecting code):
(doseq [subscriber (:subscribers new-state)] (replace-canvas (:canvas new-state) subscriber))
note (:canvas new-state)
more idiomatic (new-state :canvas)
- keyword first.
Comments
Post a Comment