Skip to content

Commit deec44a

Browse files
author
dnolen
committed
CLJS-1642: cljs.core/reductions does not respect 'reduced'
Apply CLJ-1185 patch to ClojureScript
1 parent 39083a6 commit deec44a

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8858,15 +8858,17 @@ reduces them without incurring seq initialization"
88588858
"Returns a lazy seq of the intermediate values of the reduction (as
88598859
per reduce) of coll by f, starting with init."
88608860
([f coll]
8861-
(lazy-seq
8862-
(if-let [s (seq coll)]
8863-
(reductions f (first s) (rest s))
8864-
(list (f)))))
8861+
(lazy-seq
8862+
(if-let [s (seq coll)]
8863+
(reductions f (first s) (rest s))
8864+
(list (f)))))
88658865
([f init coll]
8866+
(if (reduced? init)
8867+
(list @init)
88668868
(cons init
8867-
(lazy-seq
8868-
(when-let [s (seq coll)]
8869-
(reductions f (f init (first s)) (rest s)))))))
8869+
(lazy-seq
8870+
(when-let [s (seq coll)]
8871+
(reductions f (f init (first s)) (rest s))))))))
88708872

88718873
(defn juxt
88728874
"Takes a set of functions and returns a fn that is the juxtaposition

src/test/cljs/cljs/core_test.cljs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,21 @@
31503150
(is (= [""] (s/split-lines "")))
31513151
(is (= [] (s/split-lines "\n\n\n"))))
31523152

3153+
(deftest test-reductions-obeys-reduced
3154+
(is (= [0 :x]
3155+
(reductions (constantly (reduced :x))
3156+
(range))))
3157+
(is (= [:x]
3158+
(reductions (fn [acc x] x)
3159+
(reduced :x)
3160+
(range))))
3161+
(is (= [2 6 12 12]
3162+
(reductions (fn [acc x]
3163+
(if (= x :stop)
3164+
(reduced acc)
3165+
(+ acc x)))
3166+
[2 4 6 :stop 8 10]))))
3167+
31533168
(comment
31543169
;; ObjMap
31553170
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)