-
-
Save fogus/b3d58883ee34c4946e94c5096a8a58f0 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
(ns com.manigfeald.http2.cont) | |
(defn bind% [m f] | |
(fn [kont bundle] | |
(m (fn [value] ((f value) kont bundle)) bundle))) | |
(defmacro return% [value] | |
`(fn [kont# bundle#] | |
((try | |
(let [v# ~value] | |
#(kont# v#)) | |
(catch Throwable t# | |
(prn t#) | |
(if (:handler bundle#) | |
#((:handler bundle#) t#) | |
#(throw t#))))))) | |
(defmacro let-cont% [bindings & body] | |
(assert (even? (count bindings))) | |
(if (seq bindings) | |
(let [[n v & bindings] bindings] | |
`(bind% ~v (fn ~'g [~n] (let-cont% [~@bindings] ~@body)))) | |
`(do ~@body))) | |
(defn try% [action handler] | |
(fn [kont bundle] | |
(action kont (assoc bundle :handler (fn [t] ((handler t) kont bundle)))))) | |
(def call-with-cc% identity) | |
(defn label% [] | |
(fn here [kont bundle] | |
(kont | |
(fn [] | |
(fn [_ _] | |
(here kont bundle)))))) | |
(defn goto% [label] (label)) | |
(defn loop% [v] | |
(partial (fn here [v kont bundle] | |
(kont | |
[v | |
(fn [v] | |
(fn [_ _] | |
(here v kont bundle)))])) | |
v)) | |
(defn recur% [label v] (label v)) | |
(defn -loop% [& v] | |
(partial (fn here [v kont bundle] | |
(kont | |
(cons (fn [v] | |
(fn [_ _] | |
(here v kont bundle))) | |
v))) | |
v)) | |
(defn -recur% [label & v] (label v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment