Skip to content

Commit 9af5a49

Browse files
committed
Turn async-for into macros
1 parent ae9da51 commit 9af5a49

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/manifold/deferred.clj

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,24 +1513,27 @@
15131513
(prefer-method print-method IDeferred IDeref)
15141514

15151515

1516-
(defn- async-for
1517-
"Retuns a CompletionStage async version of the given java function"
1518-
[original]
1519-
(fn result
1520-
([d f]
1521-
(result d f (or (ex/executor) (ex/execute-pool))))
1522-
([d f executor]
1523-
(original (onto d executor) f))))
1524-
1525-
(defn- async-for-dual
1526-
"Retuns a CompletionStage async version for the given two 2-deferred
1527-
java function"
1528-
[original]
1529-
(fn result
1530-
([d other f]
1531-
(result d other f (or (ex/executor) (ex/execute-pool))))
1532-
([d other f executor]
1533-
(original (onto d executor) other f))))
1516+
(defmacro ^:no-doc def-async-for
1517+
"Defines a CompletionStage async version of the function associated with
1518+
the given symbol."
1519+
[fn-name]
1520+
(let [async-name (symbol (str (name fn-name) "-async"))]
1521+
`(defn ~async-name
1522+
([d# f#]
1523+
(~async-name d# f# (or (ex/executor) (ex/execute-pool))))
1524+
([d# f# executor#]
1525+
(~fn-name (onto d# executor#) f#)))))
1526+
1527+
(defmacro ^:no-doc def-async-for-dual
1528+
"Defines a CompletionStage async version of the two-deferred
1529+
function associated with the given symbol."
1530+
[fn-name]
1531+
(let [async-name (symbol (str (name fn-name) "-async"))]
1532+
`(defn ~async-name
1533+
([d# d2# f#]
1534+
(~async-name d# d2# f# (or (ex/executor) (ex/execute-pool))))
1535+
([d# d2# f# executor#]
1536+
(~fn-name (onto d# executor#) d2# f#)))))
15341537

15351538
(defmacro ^:no-doc assert-some
15361539
"Throws NullPointerException if any of the arguments is null."
@@ -1567,35 +1570,35 @@
15671570
(assert-some f)
15681571
(map-deferred d #(.apply f %)))
15691572

1570-
(def ^:private then-apply-async (async-for then-apply))
1573+
(def-async-for then-apply)
15711574

15721575
(defn- then-accept [d ^Consumer c]
15731576
(assert-some c)
15741577
(map-deferred d #(.accept c %)))
15751578

1576-
(def ^:private then-accept-async (async-for then-accept))
1579+
(def-async-for then-accept)
15771580

15781581
(defn- then-run [d ^Runnable f]
15791582
(assert-some f)
15801583
(map-deferred d (fn [_] (.run f))))
15811584

1582-
(def ^:private then-run-async (async-for then-run))
1585+
(def-async-for then-run)
15831586

15841587

15851588
(defn- then-combine [d other ^BiFunction f]
15861589
(assert-some other f)
15871590
(map-deferred (zip d other)
15881591
(fn [[x y]] (.apply f x y))))
15891592

1590-
(def ^:private then-combine-async (async-for-dual then-combine))
1593+
(def-async-for-dual then-combine)
15911594

15921595

15931596
(defn- then-accept-both [d other ^BiConsumer f]
15941597
(assert-some other f)
15951598
(map-deferred (zip d other)
15961599
(fn [[x y]] (.accept f x y))))
15971600

1598-
(def ^:private then-accept-both-async (async-for-dual then-accept-both))
1601+
(def-async-for-dual then-accept-both)
15991602

16001603

16011604
(defn- run-after-both [d other ^Runnable f]
@@ -1604,35 +1607,35 @@
16041607
(fn [[_ _]] (.run f))))
16051608

16061609

1607-
(def ^:private run-after-both-async (async-for-dual run-after-both))
1610+
(def-async-for-dual run-after-both)
16081611

16091612

16101613
(defn- apply-to-either [d other ^java.util.function.Function f]
16111614
(assert-some other f)
16121615
(then-apply (alt d other) f))
16131616

1614-
(def ^:private apply-to-either-async (async-for-dual apply-to-either))
1617+
(def-async-for-dual apply-to-either)
16151618

16161619

16171620
(defn- accept-either [d other ^java.util.function.Function f]
16181621
(assert-some other f)
16191622
(then-accept (alt d other) f))
16201623

1621-
(def ^:private accept-either-async (async-for-dual accept-either))
1624+
(def-async-for-dual accept-either)
16221625

16231626

16241627
(defn- run-after-either [d other ^java.util.function.Function f]
16251628
(assert-some other f)
16261629
(then-run (alt d other) f))
16271630

1628-
(def ^:private run-after-either-async (async-for-dual run-after-either))
1631+
(def-async-for-dual run-after-either)
16291632

16301633

16311634
(defn- then-compose [d ^java.util.function.Function f]
16321635
(assert-some f)
16331636
(mapcat-deferred d (fn [value] (.apply f value))))
16341637

1635-
(def ^:private then-compose-async (async-for then-compose))
1638+
(def-async-for then-compose)
16361639

16371640

16381641
(defn- then-handle [d ^java.util.function.BiFunction f]
@@ -1644,7 +1647,8 @@
16441647
(fn [error] (success! d' (.apply f nil error))))
16451648
d'))
16461649

1647-
(def ^:private then-handle-async (async-for then-handle))
1650+
1651+
(def-async-for then-handle)
16481652

16491653

16501654
(defn- then-exceptionally [d ^java.util.function.Function f]
@@ -1682,7 +1686,7 @@
16821686
(error! d' err)))))
16831687
d'))
16841688

1685-
(def ^:private when-complete-async (async-for when-complete))
1689+
(def-async-for when-complete)
16861690

16871691
;;;
16881692

0 commit comments

Comments
 (0)