|
725 | 725 |
|
726 | 726 | (declare chain)
|
727 | 727 |
|
728 |
| -(defn unwrap' [x] |
| 728 | +(defn unwrap' |
| 729 | + "Like unwrap, but does not coerce deferrable values." |
| 730 | + [x] |
729 | 731 | (if (deferred? x)
|
730 | 732 | (let [val (success-value x ::none)]
|
731 | 733 | (if (identical? val ::none)
|
732 | 734 | x
|
733 | 735 | (recur val)))
|
734 | 736 | x))
|
735 | 737 |
|
736 |
| -(defn unwrap [x] |
| 738 | +(defn unwrap |
| 739 | + "Recursively unwraps a deferred or deferrable until either 1) a non-deferred |
| 740 | + value is reached, or 2) an unrealized deferrable is reached." |
| 741 | + [x] |
737 | 742 | (let [d (->deferred x nil)]
|
738 | 743 | (if (nil? d)
|
739 | 744 | x
|
|
1041 | 1046 | @(chain 1 inc #(future (inc %))) => 3
|
1042 | 1047 |
|
1043 | 1048 | @(chain (future 1) inc inc) => 3
|
1044 |
| -
|
1045 | 1049 | "
|
1046 | 1050 | {:inline (fn [& args]
|
1047 | 1051 | (if false #_(< 3 (count args))
|
|
1207 | 1211 |
|
1208 | 1212 | @(zip 1 2 3) => [1 2 3]
|
1209 | 1213 | @(zip (future 1) 2 3) => [1 2 3]
|
1210 |
| -
|
1211 | 1214 | "
|
1212 | 1215 | {:inline (fn [x] `(chain ~x vector))
|
1213 | 1216 | :inline-arities #{1}}
|
|
1313 | 1316 |
|
1314 | 1317 | (defmacro loop
|
1315 | 1318 | "A version of Clojure's loop which allows for asynchronous loops, via `manifold.deferred/recur`.
|
1316 |
| - `loop` will always return a deferred value, even if the body is synchronous. Note that `loop` does **not** coerce values to deferreds, actual Manifold deferreds must be used. |
| 1319 | + `loop` will always return a deferred value, even if the body is synchronous. Note that `loop` |
| 1320 | + does **not** coerce values to deferreds, actual Manifold deferreds must be used. |
1317 | 1321 |
|
1318 | 1322 | (loop [i 1e6]
|
1319 | 1323 | (chain (future i)
|
|
1497 | 1501 | bindings
|
1498 | 1502 | body))
|
1499 | 1503 |
|
| 1504 | + |
| 1505 | + |
1500 | 1506 | (defmethod print-method IDeferred [o ^Writer w]
|
1501 | 1507 | (.write w
|
1502 | 1508 | (str
|
|
1513 | 1519 | (prefer-method print-method IDeferred IDeref)
|
1514 | 1520 |
|
1515 | 1521 |
|
| 1522 | + |
| 1523 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1524 | +;; CompletionStage helper fns |
| 1525 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1526 | + |
1516 | 1527 | (defmacro ^:no-doc def-async-for
|
1517 | 1528 | "Defines a CompletionStage async version of the function associated with
|
1518 |
| - the given symbol." |
| 1529 | + the given symbol, with '-async' appended." |
1519 | 1530 | [fn-name]
|
1520 | 1531 | (let [async-name (symbol (str (name fn-name) "-async"))]
|
1521 | 1532 | `(defn- ~async-name
|
|
1526 | 1537 |
|
1527 | 1538 | (defmacro ^:no-doc def-async-for-dual
|
1528 | 1539 | "Defines a CompletionStage async version of the two-deferred
|
1529 |
| - function associated with the given symbol." |
| 1540 | + function associated with the given symbol, with '-async' appended." |
1530 | 1541 | [fn-name]
|
1531 | 1542 | (let [async-name (symbol (str (name fn-name) "-async"))]
|
1532 | 1543 | `(defn- ~async-name
|
|
1536 | 1547 | (~fn-name (onto d# executor#) d2# f#)))))
|
1537 | 1548 |
|
1538 | 1549 | (defn- fmap-deferred
|
1539 |
| - "Like map/fmap but for deferreds. |
| 1550 | + "Returns a new deferred with function `f` applies to realized value of `d`. |
| 1551 | + (Like fmap but for deferreds.) |
1540 | 1552 |
|
1541 |
| - This function does not unwrap the result of f" |
| 1553 | + This function does not unwrap the result of f; it will only be applied to |
| 1554 | + the immediate value of `d`. This is for mimicking CompletionStage's |
| 1555 | + behavior." |
1542 | 1556 | [d f]
|
1543 | 1557 | (let [d' (deferred)]
|
1544 | 1558 | (on-realized d
|
1545 | 1559 | (fn [val] (success! d' (f val)))
|
1546 | 1560 | (fn [error] (error! d' error)))
|
1547 | 1561 | d'))
|
1548 | 1562 |
|
1549 |
| - |
1550 | 1563 | (defn- then-apply [d ^Function f]
|
1551 | 1564 | (assert-some f)
|
1552 | 1565 | (fmap-deferred d #(.apply f %)))
|
|
0 commit comments