Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created July 15, 2011 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/1084860 to your computer and use it in GitHub Desktop.
Save jcromartie/1084860 to your computer and use it in GitHub Desktop.
(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)
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