Step 4: lazy mapcat overlay — eliminate apply forcing on infinite inputs

mapcat in 10-seq.clj now uses a defn- mapcat-step helper that walks
lazy map results element-by-element (cons + lazy-seq), with explicit
arities for 1/2/3/4+ colls. The 4+-coll arity still uses apply to
spread colls to map, but apply no longer forces the inner map result.

Previously (apply concat (apply map f colls)) forced the lazy map
result through core-apply realize-for-iteration, which hung on
infinite inputs. The new step-based impl walks one element at a time
via first/rest/seq primitives.

Re-enabled deferred mapcat infinite-input harness case. 19/19 pass.

Gates: lazy-infinite 19/19, conformance 229/229 interpret+self-host
(compile 228/229 — 1 pre-existing protocol error), specs 32/32.
This commit is contained in:
Yogthos 2026-06-08 08:14:59 -04:00
parent e2e189acfe
commit d16e1f4eba
2 changed files with 25 additions and 5 deletions

View file

@ -23,7 +23,29 @@
(recur (conj ret (first s)) (next s))
(seq ret))))
(defn- mapcat-step [rs cur]
(lazy-seq
(if cur
(let [s (seq cur)]
(if s
(cons (first s) (mapcat-step rs (rest s)))
(mapcat-step rs nil)))
(let [s (seq rs)]
(if s
(let [c (first s)
sc (seq c)]
(if sc
(cons (first sc) (mapcat-step (rest s) (rest sc)))
(mapcat-step (rest s) nil)))
nil)))))
(defn mapcat
([f] (comp (map f) cat))
([f & colls]
(apply concat (apply map f colls))))
([f coll]
(mapcat-step (map f coll) nil))
([f c1 c2]
(mapcat-step (map f c1 c2) nil))
([f c1 c2 c3]
(mapcat-step (map f c1 c2 c3) nil))
([f c1 c2 c3 & colls]
(mapcat-step (apply map f c1 c2 c3 colls) nil)))