Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created June 26, 2012 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjg123/2993445 to your computer and use it in GitHub Desktop.
Save mjg123/2993445 to your computer and use it in GitHub Desktop.
Clojure macro for delayed executino
(defmacro after
"Sleeps for t miliseconds then executes body. Immediately
returns a future from which the result of executing body
can (eventually) be obtained by `deref`"
{:author "Matthew Gilliard"}
[t & body]
`(future
(java.lang.Thread/sleep ~t)
~@body))
(macroexpand-1
'(after 100 (println "banana")))
(do
(let [result (after 1000
(println "fooo")
(+ 2 3 4))]
(println "done")
(println @result)))
; done
; -wait a second
; fooo
; 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment