Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created January 25, 2009 07:07
Show Gist options
  • Save hiredman/51721 to your computer and use it in GitHub Desktop.
Save hiredman/51721 to your computer and use it in GitHub Desktop.
(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