Skip to content

Commit

Permalink
Update changelog and add tests for loop/recur.
Browse files Browse the repository at this point in the history
  • Loading branch information
niwinz committed Sep 13, 2019
1 parent a185e86 commit 2cd8880
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and macros that are not heavily used):
- Add `map'`, `bind'` and `mapcat'` variants that uses inline
composition (wihtout microtasking, read more on docs).
- Make `chain` use the `map'` variant for batch computations.
- Add `loop/recur` syntax abstraction.



Expand Down
9 changes: 5 additions & 4 deletions src/promesa/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,11 @@
names (mapv first bindings)
fvals (mapv second bindings)
syms (mapv gensym names)]
`(letfn [(~INTERNAL_LOOP_FN_NAME [~@syms]
(-> (p/all [~@syms])
(p/then (fn [[~@names]] (do! ~@body)))))]
(~INTERNAL_LOOP_FN_NAME ~@fvals))))
`(do!
(letfn [(~INTERNAL_LOOP_FN_NAME [~@syms]
(-> (p/all [~@syms])
(p/then (fn [[~@names]] (do! ~@body)))))]
(~INTERNAL_LOOP_FN_NAME ~@fvals)))))

(defmacro recur
[& args]
Expand Down
9 changes: 9 additions & 0 deletions test/promesa/tests/test_core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,12 @@
#?(:cljs (t/async done (p/do! (test) (done)))
:clj @(test))))

(t/deftest loop-and-recur
(let [p1 (p/loop [a (p/delay 50 0)]
(if (= a 5)
a
(p/recur (p/delay 50 (inc a)))))
test #(->> (p/race [p1 (p/delay 400 10)])
(p/map (fn [res] (t/is (= res 5)))))]
#?(:cljs (t/async done (p/do! (test) (done)))
:clj @(test))))

0 comments on commit 2cd8880

Please sign in to comment.