Skip to content

Instantly share code, notes, and snippets.

@sordina
Last active August 29, 2015 14:04
Show Gist options
  • Save sordina/6b9faec46bb6f2352ce0 to your computer and use it in GitHub Desktop.
Save sordina/6b9faec46bb6f2352ce0 to your computer and use it in GitHub Desktop.
(deftype PromiseWrapper [r fun prom]
clojure.lang.IDeref
(deref [this]
(dosync
(let [current @r]
(if current (:value current)
(let [result (fun @prom)]
(ref-set r {:value result})
result))))))
(defmacro fmap [fun prom]
`(PromiseWrapper. (ref nil) ~fun ~prom))
(defmacro then [a b & body]
`(fmap (fn [r#] (let [~b r#] ~@body)) ~a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment