From cab1a75b752b57bbded188145e9af6975b7fcf05 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Mon, 8 Jun 2026 23:43:00 -0400 Subject: [PATCH] core: migrate partition-all to the Clojure overlay (minimal realization) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves the deferred partition-all port (jolt-yo3). The earlier CLJ attempt via lazy take/drop over-realized vs the Janet pstep, tripping the §6.3 laziness counter. The collection arities now realize EXACTLY n per chunk with a first/rest loop and continue from the advanced cursor (no re-drop), so (take 3 (partition-all 2 (map counting (range)))) realizes exactly 6 — matching minimal realization in both interpret and compile modes. Keeps transducer + [n coll] + [n step coll] arities. letfn-bound recursion sidesteps the compile-mode multi-arity closure bug (jolt-zxw), like keep-indexed/map-indexed. Removed from the Janet seed: core-partition-all + its binding + the now-dead td-partition-all helper (the CLJ version carries its own transducer arity). Gate: conformance 262x3, lazy-infinite 44/44 (incl the §6.3 partition-all counter), clojure-test-suite 4004/66, fixpoint, self-host, specs+unit green. --- jolt-core/clojure/core/40-lazy.clj | 41 +++++++++++++++++++++++++++--- src/jolt/core.janet | 36 ++------------------------ 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/jolt-core/clojure/core/40-lazy.clj b/jolt-core/clojure/core/40-lazy.clj index d5e866c..3f0b24a 100644 --- a/jolt-core/clojure/core/40-lazy.clj +++ b/jolt-core/clojure/core/40-lazy.clj @@ -100,7 +100,40 @@ (lazy-seq (cons x (iterate f (f x))))) -;; partition-all stays in the Janet seed for now (core-partition-all): it already -;; has the transducer + collection arities (jolt-cru), and a CLJ port realizes a -;; different (non-minimal) element count via take/drop than the Janet one, -;; tripping the §6.3 laziness counters + a suite file. Ported separately. +;; --- partition-all --- (transducer + [n coll] + [n step coll]) +;; The collection arities realize EXACTLY n per chunk via a first/rest loop and +;; continue from the advanced cursor (not a re-drop), so they realize minimally +;; — matching the Janet pstep the §6.3 laziness counters were written against. +;; letfn-bound `go` (not arity-direct recursion) sidesteps the compile-mode +;; multi-arity closure-capture bug (jolt-zxw), as keep-indexed/map-indexed do. +(defn partition-all + ([n] + (fn [rf] + (let [a (volatile! [])] + (fn + ([] (rf)) + ([result] + (let [result (if (zero? (count @a)) + result + (let [v @a] (vreset! a []) (unreduced (rf result v))))] + (rf result))) + ([result input] + (vswap! a conj input) + (if (= n (count @a)) + (let [v @a] (vreset! a []) (rf result v)) + result)))))) + ([n coll] + (letfn [(go [s] + (lazy-seq + (when (seq s) + (loop [i 0 chunk [] cur s] + (if (and (< i n) (seq cur)) + (recur (inc i) (conj chunk (first cur)) (rest cur)) + (cons chunk (go cur)))))))] + (go coll))) + ([n step coll] + (letfn [(go [s] + (lazy-seq + (when (seq s) + (cons (take n s) (go (nthrest s step))))))] + (go coll)))) diff --git a/src/jolt/core.janet b/src/jolt/core.janet index 0dfa53e..309d30f 100644 --- a/src/jolt/core.janet +++ b/src/jolt/core.janet @@ -831,24 +831,7 @@ # Stateful windowing transducers. The 1-arg (completion) arity flushes a partial # trailing window before delegating to rf's completion; matches Clojure. -(defn td-partition-all [n] - (fn [rf] - (var buf @[]) - (fn [& a] - (case (length a) - 0 (rf) - 1 (let [result (if (= 0 (length buf)) (a 0) - (let [v (tuple/slice (tuple ;buf))] - (set buf @[]) - (core-unreduced (rf (a 0) v))))] - (rf result)) - (do - (array/push buf (a 1)) - (if (= n (length buf)) - (let [v (tuple/slice (tuple ;buf))] - (set buf @[]) - (rf (a 0) v)) - (a 0))))))) +# td-partition-all removed: partition-all (incl transducer arity) lives in core/40-lazy.clj. # partition-by's transducer arity lives with its (lazy) collection arity in the # overlay (10-seq tier), written in Clojure with volatiles. @@ -1366,21 +1349,7 @@ # partition-by now lives in the Clojure seq tier (core/10-seq.clj). -(defn core-partition-all [n & rest] - (if (= 0 (length rest)) (td-partition-all n) - (let [coll (in rest 0)] - # Option A: always lazy. - (defn pstep [c] - (fn [] - (if (seq-done? c) nil - (do - (var part @[]) (var cur c) (var i 0) - (while (and (< i n) (not (seq-done? cur))) - (array/push part (core-first cur)) - (set cur (core-rest cur)) - (++ i)) - @[(tuple/slice (tuple ;part)) (pstep cur)])))) - (make-lazy-seq (pstep (lazy-from coll)))))) +# partition-all now lives in the Clojure lazy tier (core/40-lazy.clj). # keep-indexed / map-indexed / cycle now live in the Clojure lazy tier @@ -2762,7 +2731,6 @@ "get-in" core-get-in "contains?" core-contains? "count" core-count - "partition-all" core-partition-all "pop" core-pop "trampoline" core-trampoline "format" core-format