Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created June 22, 2022 21:03
Show Gist options
  • Save hiredman/bc54694fbe15b9589b5a662ba88badd5 to your computer and use it in GitHub Desktop.
Save hiredman/bc54694fbe15b9589b5a662ba88badd5 to your computer and use it in GitHub Desktop.
(defmacro return% [a]
`(fn return-fn# [success# error#]
#(try
(success# ~a)
(catch Throwable t#
(error# t#)))))
(defn bind% [m b]
(fn a [success error]
#(m (fn x [value] ((b value) success error)) error)))
(defn try% [m handler]
(fn [success error]
(let [new-error (handler error)]
#(m success new-error))))
(defmacro let% [bindings body]
(if (seq bindings)
(let [[name value & bindings] bindings]
(if (= name '!)
`(do ~value (let% ~bindings ~body))
`(bind% ~value (fn bindfn# [~name] (let% ~bindings ~body)))))
body))
(def call-cc% identity)
(trampoline
(let% [[success error n] (call-cc% (fn [success error]
(success
[(fn [value] (return% (success value)))
(fn [value] (return% (error value)))
0])))
_ (return% (prn n))]
(if (> 10 n)
(success [success error (inc n)])
(return% nil)))
identity
prn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment