Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created September 10, 2016 18:41
Show Gist options
  • Select an option

  • Save noisesmith/268087fbce11dbab541185b13dd4ee54 to your computer and use it in GitHub Desktop.

Select an option

Save noisesmith/268087fbce11dbab541185b13dd4ee54 to your computer and use it in GitHub Desktop.
using add-watch to track past states of an atom
Clojure 1.8.0
+user=> (def a (atom nil))
#'user/a
+user=> (def s (atom []))
#'user/s
+user=> (add-watch a :state-watcher (fn [_ _ _ n] (swap! s conj n)))
#object[clojure.lang.Atom 0x928763c {:status :ready, :val nil}]
+user=> (swap! a conj :initial)
(:initial)
+user=> @s
[(:initial)]
+user=> (swap! a conj :next-value)
(:next-value :initial)
+user=> @s
[(:initial) (:next-value :initial)]
+user=> (reset! a :whatever)
:whatever
+user=> @s
[(:initial) (:next-value :initial) :whatever]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment