Skip to content

Instantly share code, notes, and snippets.

@latant
Created July 25, 2020 00:33
Show Gist options
  • Save latant/734b00204d0c5cc480a114f89e22021b to your computer and use it in GitHub Desktop.
Save latant/734b00204d0c5cc480a114f89e22021b to your computer and use it in GitHub Desktop.
An abstraction over reactive behavior
(defmulti then-fn
(fn [deferred _] (class deferred)))
(defmethod then-fn CompletableFuture [^CompletableFuture future func]
(.thenAccept future
(reify Consumer
(accept [_ result] (func result)))))
(defmacro async [arg & block]
(if (vector? arg)
(let [[result deferred] arg]
`(then-fn ~deferred (fn [~result] ~@block)))
`(then-fn ~arg (fn [~'_] ~@block))))
(defn double-num-async [n]
(CompletableFuture/completedFuture (* 2 n)))
(defn -main []
(async [x (double-num-async 2)]
(println x))
(async (double-num-async 3)
(println "done")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment