Created
June 26, 2012 05:08
-
-
Save mjg123/2993445 to your computer and use it in GitHub Desktop.
Clojure macro for delayed executino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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