-
-
Save fogus/9415706 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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