Created
July 25, 2020 00:33
-
-
Save latant/734b00204d0c5cc480a114f89e22021b to your computer and use it in GitHub Desktop.
An abstraction over reactive behavior
This file contains hidden or 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
(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