Skip to content

Instantly share code, notes, and snippets.

@jcromartie jcromartie/watch.clj
Created Jul 15, 2011

What would you like to do?
(defn watch
"Returns (running) thread which observes the value of f every delay
milliseconds, and calls the callback fn if the value changes. callback
will be called with the old and new values from f"
([f callback delay]
(let [proc (fn []
(loop [x1 ::watch-init]
(let [x2 (f)]
(when (not= x1 x2)
(callback x1 x2))
(Thread/sleep delay)
(recur x2))))
thread (Thread. proc)]
(.start thread)
([f callback]
(watch f callback 5000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.