Created
April 3, 2010 16:54
-
-
Save pervognsen/354676 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
(defn label* [body] | |
(let [payload (atom nil)] | |
(try | |
(let [result (body (fn [& [x]] | |
(if (compare-and-set! payload nil [x]) | |
(throw (Exception.)) ;; It's a pain in the ass to gen-class custom Exception subclass. | |
(throw (Exception. "Tried to jump outside extent.")))))] | |
(swap! payload (fn [_] [])) | |
result) | |
(catch Exception e | |
(if (seq @payload) | |
(first @payload) | |
(throw e)))))) | |
(defmacro label [name & body] | |
`(label* (fn [~name] ~@body))) | |
;; Examples | |
(label break | |
(println "before") | |
(break 42) | |
(println "after")) | |
(label break | |
(doseq [x (range 10)] | |
(label continue | |
(doseq [y (range 10)] | |
(if (>= y 5) | |
(if (>= x 5) | |
(break 42) | |
(continue)) | |
(println "x:" x "y:" y)))))) | |
((label break break) 42) ;; "Tried to jump outside extent." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment