Skip to content

Commit 9dc57c6

Browse files
thomasmulvaneyswannodette
authored andcommitted
CLJS-2128: Fix regression in CLJS-1886
Also adds fallback for Subvec.reduce when backing vector is not a APersistentVector
1 parent f406b8a commit 9dc57c6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5534,9 +5534,13 @@ reduces them without incurring seq initialization"
55345534

55355535
IReduce
55365536
(-reduce [coll f]
5537-
(pv-reduce v f start end))
5537+
(if (implements? APersistentVector v)
5538+
(pv-reduce v f start end)
5539+
(ci-reduce coll f)))
55385540
(-reduce [coll f init]
5539-
(pv-reduce v f init start end))
5541+
(if (implements? APersistentVector v)
5542+
(pv-reduce v f init start end)
5543+
(ci-reduce coll f init)))
55405544

55415545
IKVReduce
55425546
(-kv-reduce [coll f init]

src/test/cljs/cljs/collections_test.cljs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,26 @@
660660
(is (= metadata (meta k'))))
661661
(let [map (sorted-map nil :foo)]
662662
(is (= (find map nil) [nil :foo])))))
663+
664+
(deftype CustomVectorThing [v]
665+
IVector
666+
(-assoc-n [coll i val] (assoc-n v i val))
667+
668+
IIndexed
669+
(-nth [coll i] (nth v i))
670+
(-nth [coll i not-found] (nth v i not-found))
671+
672+
ICounted
673+
(-count [coll] (count v)))
674+
675+
(deftest test-cljs-2128
676+
(testing "Subvec iteration"
677+
(testing "Subvec over PersistentVector uses RangedIterator"
678+
(is (instance? RangedIterator (-iterator (subvec [0 1 2 3] 1 3)))))
679+
(testing "Subvec over other vectors uses naive SeqIter"
680+
(is (instance? SeqIter (-iterator (subvec (->CustomVectorThing [0 1 2 3]) 1 3))))))
681+
(testing "Subvec reduce"
682+
(testing "Subvec over PersistentVector reduces as expected"
683+
(is (= [1 2] (reduce conj [] (subvec [0 1 2 3] 1 3)))))
684+
(testing "Subvec over other vectors reduces as expected"
685+
(is (= [1 2] (reduce conj [] (subvec (->CustomVectorThing [0 1 2 3]) 1 3)))))))

0 commit comments

Comments
 (0)