Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created April 3, 2010 16:54
Show Gist options
  • Save pervognsen/354676 to your computer and use it in GitHub Desktop.
Save pervognsen/354676 to your computer and use it in GitHub Desktop.
(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