Skip to content

Commit 6b97ce4

Browse files
michalmarczykdnolen
authored andcommitted
CLJS-977: implement IKVReduce in Subvec
1 parent 98702e7 commit 6b97ce4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cljs/cljs/core.cljs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,16 @@ reduces them without incurring seq initialization"
49494949
(-reduce [coll f start]
49504950
(ci-reduce coll f start))
49514951

4952+
IKVReduce
4953+
(-kv-reduce [coll f init]
4954+
(loop [i start j 0 init init]
4955+
(if (< i end)
4956+
(let [init (f init j (-nth v i))]
4957+
(if (reduced? init)
4958+
@init
4959+
(recur (inc i) (inc j) init)))
4960+
init)))
4961+
49524962
IFn
49534963
(-invoke [coll k]
49544964
(-nth coll k))

test/cljs/cljs/core_test.cljs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@
459459
;; no layered subvecs
460460
(is (identical? v1 (.-v (subvec s 1 4))))
461461
(let [m {:x 1}]
462-
(is (= m (meta (with-meta s m))))))
462+
(is (= m (meta (with-meta s m)))))
463+
;; CLJS-997
464+
(is (= (reduce-kv + 123 (vec (range 10 50)))
465+
(reduce-kv + 123 (subvec (vec (range 100)) 10 50)))))
463466
;; CLJS-513
464467
(let [sentinel (js-obj)
465468
s (subvec [0 1 2 3] 1 2)]

0 commit comments

Comments
 (0)