From e70edc3b950d5a84965b74d60d1ab4cf0d9090f8 Mon Sep 17 00:00:00 2001 From: davidnolen Date: Mon, 21 Oct 2024 07:27:49 -0400 Subject: [PATCH] CLJS-3240: lazy-seq does not cache result of calling seq patch adapted from Ambrose Bonnaire-Sergeant --- src/main/cljs/cljs/core.cljs | 30 ++++++++++-------------- src/test/cljs/cljs/collections_test.cljs | 8 +++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 73bd921f2..3d6ea2f1f 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -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] @@ -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)) @@ -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)) @@ -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)) diff --git a/src/test/cljs/cljs/collections_test.cljs b/src/test/cljs/cljs/collections_test.cljs index d66a87cda..04d89d4aa 100644 --- a/src/test/cljs/cljs/collections_test.cljs +++ b/src/test/cljs/cljs/collections_test.cljs @@ -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)