Skip to content

Commit

Permalink
CLJS-3240: lazy-seq does not cache result of calling seq
Browse files Browse the repository at this point in the history
patch adapted from Ambrose Bonnaire-Sergeant
  • Loading branch information
swannodette committed Oct 21, 2024
1 parent f1fc819 commit e70edc3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3514,7 +3514,10 @@ reduces them without incurring seq initialization"
(if (nil? fn)
s
(do
(set! s (fn))
(loop [ls (fn)]
(if (instance? LazySeq ls)
(recur (.sval ls))
(set! s (seq ls))))
(set! fn nil)
s)))
(indexOf [coll x]
Expand All @@ -3534,27 +3537,27 @@ reduces them without incurring seq initialization"
(-with-meta [coll new-meta]
(if (identical? new-meta meta)
coll
(LazySeq. new-meta #(-seq coll) nil __hash)))
(LazySeq. new-meta #(.sval coll) nil __hash)))

IMeta
(-meta [coll] meta)

ISeq
(-first [coll]
(-seq coll)
(.sval coll)
(when-not (nil? s)
(first s)))
(-first s)))
(-rest [coll]
(-seq coll)
(.sval coll)
(if-not (nil? s)
(rest s)
(-rest s)
()))

INext
(-next [coll]
(-seq coll)
(.sval coll)
(when-not (nil? s)
(next s)))
(-next s)))

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

ISeqable
(-seq [coll]
(.sval coll)
(when-not (nil? s)
(loop [ls s]
(if (instance? LazySeq ls)
(recur (.sval ls))
(do (set! s ls)
(seq s))))))
(-seq [coll] (.sval coll))

IReduce
(-reduce [coll f] (seq-reduce f coll))
Expand Down Expand Up @@ -7216,7 +7212,7 @@ reduces them without incurring seq initialization"
extra-kvs (seq trailing)
ret (make-array (+ seed-cnt (* 2 (count extra-kvs))))
ret (array-copy seed 0 ret 0 seed-cnt)]
(loop [i seed-cnt extra-kvs extra-kvs]
(loop [i seed-cnt extra-kvs extra-kvs]00
(if extra-kvs
(let [kv (first extra-kvs)]
(aset ret i (-key kv))
Expand Down
8 changes: 8 additions & 0 deletions src/test/cljs/cljs/collections_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,14 @@
(deftest test-cljs-3393
(is (= '(0 2 4) (take 3 (filter even? (range 100000000))))))

(deftest test-cljs-3420-lazy-seq-caching-bug
(testing "LazySeq should realize seq once"
(let [a (atom 0)
x (eduction (map (fn [_] (swap! a inc))) [nil])
l (lazy-seq x)]
(dotimes [_ 10]
(is (= [1] l))))))

(comment

(run-tests)
Expand Down

0 comments on commit e70edc3

Please sign in to comment.