Created
January 25, 2009 07:07
-
-
Save hiredman/51721 to your computer and use it in GitHub Desktop.
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
(declare *fails* *succeeds* *exits*) | |
(def *horizon* nil) | |
(defmacro when-hrz [which & forms] | |
`(do | |
(when (not *horizon*) (throw (RuntimeException. "Naked Singularity"))) | |
(swap! | |
~(condp = which :fails '*fails* :succeeds '*succeeds* :exits '*exits*) | |
conj (fn [] ~@forms)))) | |
(defmacro horizon [& body] | |
`(binding [*horizon* true | |
*fails* (atom (list)) | |
*succeeds* (atom (list)) | |
*exits* (atom (list))] | |
(try | |
(let [y# (do ~@body)] (dorun (map (fn[x#](x#)) @*succeeds*)) y#) | |
(catch Exception e# (dorun (map (fn[x#] (x#)) @*fails*)) | |
(throw e#)) | |
(finally | |
(dorun (map (fn[x#] (x#)) @*exits*)))))) | |
(defn foo [] | |
(when-hrz :fails (prn "failed")) | |
(when-hrz :succeeds (prn "succeeded")) | |
(when-hrz :exits (prn "exited"))) | |
(horizon | |
(foo) | |
42) | |
(horizon | |
(foo) | |
(throw (Exception. "err")) | |
42) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment