Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLJS-3240: lazy-seq does not cache result of calling seq #237

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading