Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from hiredman/cont.clj
Created February 8, 2021 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/b3d58883ee34c4946e94c5096a8a58f0 to your computer and use it in GitHub Desktop.
Save fogus/b3d58883ee34c4946e94c5096a8a58f0 to your computer and use it in GitHub Desktop.
(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