Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from alandipert/callcc.clj
Created March 7, 2014 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/9415706 to your computer and use it in GitHub Desktop.
Save fogus/9415706 to your computer and use it in GitHub Desktop.
(import 'clojure.lang.ExceptionInfo)
(defn call-cc [f]
(let [v (promise)
e (ex-info "cc" {:v v})]
(try (f #(do (deliver v %) (throw e)))
(catch ExceptionInfo ex
(if (identical? ex e)
@(:v (ex-data ex))
(throw ex))))))
(defn search [pred coll]
(call-cc
(fn [return]
(doseq [el coll]
(if (pred el)
(return el))))))
(search even? [1 2 3]) ;=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment