Skip to content

Commit 2c5f442

Browse files
authored
CLJS-3240: lazy-seq does not cache result of calling seq (#237)
CLJS-3240: lazy-seq does not cache result of calling seq - patch adapted from Ambrose Bonnaire-Sergeant
1 parent f1fc819 commit 2c5f442

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/main/cljs/cljs/core.cljs

+13-17
Original file line numberDiff line numberDiff line change
@@ -3514,7 +3514,10 @@ reduces them without incurring seq initialization"
35143514
(if (nil? fn)
35153515
s
35163516
(do
3517-
(set! s (fn))
3517+
(loop [ls (fn)]
3518+
(if (instance? LazySeq ls)
3519+
(recur (.sval ls))
3520+
(set! s (seq ls))))
35183521
(set! fn nil)
35193522
s)))
35203523
(indexOf [coll x]
@@ -3534,27 +3537,27 @@ reduces them without incurring seq initialization"
35343537
(-with-meta [coll new-meta]
35353538
(if (identical? new-meta meta)
35363539
coll
3537-
(LazySeq. new-meta #(-seq coll) nil __hash)))
3540+
(LazySeq. new-meta #(.sval coll) nil __hash)))
35383541

35393542
IMeta
35403543
(-meta [coll] meta)
35413544

35423545
ISeq
35433546
(-first [coll]
3544-
(-seq coll)
3547+
(.sval coll)
35453548
(when-not (nil? s)
3546-
(first s)))
3549+
(-first s)))
35473550
(-rest [coll]
3548-
(-seq coll)
3551+
(.sval coll)
35493552
(if-not (nil? s)
3550-
(rest s)
3553+
(-rest s)
35513554
()))
35523555

35533556
INext
35543557
(-next [coll]
3555-
(-seq coll)
3558+
(.sval coll)
35563559
(when-not (nil? s)
3557-
(next s)))
3560+
(-next s)))
35583561

35593562
ICollection
35603563
(-conj [coll o] (cons o coll))
@@ -3570,14 +3573,7 @@ reduces them without incurring seq initialization"
35703573
(-hash [coll] (caching-hash coll hash-ordered-coll __hash))
35713574

35723575
ISeqable
3573-
(-seq [coll]
3574-
(.sval coll)
3575-
(when-not (nil? s)
3576-
(loop [ls s]
3577-
(if (instance? LazySeq ls)
3578-
(recur (.sval ls))
3579-
(do (set! s ls)
3580-
(seq s))))))
3576+
(-seq [coll] (.sval coll))
35813577

35823578
IReduce
35833579
(-reduce [coll f] (seq-reduce f coll))
@@ -7216,7 +7212,7 @@ reduces them without incurring seq initialization"
72167212
extra-kvs (seq trailing)
72177213
ret (make-array (+ seed-cnt (* 2 (count extra-kvs))))
72187214
ret (array-copy seed 0 ret 0 seed-cnt)]
7219-
(loop [i seed-cnt extra-kvs extra-kvs]
7215+
(loop [i seed-cnt extra-kvs extra-kvs]00
72207216
(if extra-kvs
72217217
(let [kv (first extra-kvs)]
72227218
(aset ret i (-key kv))

src/test/cljs/cljs/collections_test.cljs

+8
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,14 @@
11511151
(deftest test-cljs-3393
11521152
(is (= '(0 2 4) (take 3 (filter even? (range 100000000))))))
11531153

1154+
(deftest test-cljs-3420-lazy-seq-caching-bug
1155+
(testing "LazySeq should realize seq once"
1156+
(let [a (atom 0)
1157+
x (eduction (map (fn [_] (swap! a inc))) [nil])
1158+
l (lazy-seq x)]
1159+
(dotimes [_ 10]
1160+
(is (= [1] l))))))
1161+
11541162
(comment
11551163

11561164
(run-tests)

0 commit comments

Comments
 (0)