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.
139 lines
4.3 KiB
Clojure
139 lines
4.3 KiB
Clojure
;; clojure.core — lazy tier. Canonical CLJS-based lazy seq fns.
|
|
;; Loaded after 30-macros.clj, so lazy-seq macro is available.
|
|
;;
|
|
;; Each fn ported from CLJS core.cljs, stripped of chunked-seq branches.
|
|
|
|
;; --- distinct --- (transducer + lazy collection arity; value-based dedup)
|
|
(defn distinct
|
|
([]
|
|
(fn [rf]
|
|
(let [seen (volatile! #{})]
|
|
(fn ([] (rf)) ([result] (rf result))
|
|
([result input]
|
|
(if (contains? @seen input)
|
|
result
|
|
(do (vswap! seen conj input) (rf result input))))))))
|
|
([coll]
|
|
(let [step (fn step [xs seen]
|
|
(lazy-seq
|
|
((fn [[f :as xs] seen]
|
|
(when-let [s (seq xs)]
|
|
(if (contains? seen f)
|
|
(recur (rest s) seen)
|
|
(cons f (step (rest s) (conj seen f))))))
|
|
xs seen)))]
|
|
(step coll #{}))))
|
|
|
|
|
|
;; --- keep ---
|
|
(defn keep
|
|
([f]
|
|
(fn [rf]
|
|
(fn ([] (rf)) ([result] (rf result))
|
|
([result input]
|
|
(let [v (f input)]
|
|
(if (nil? v) result (rf result v)))))))
|
|
([f coll]
|
|
(lazy-seq
|
|
(when-let [s (seq coll)]
|
|
(let [x (f (first s))]
|
|
(if (nil? x)
|
|
(keep f (rest s))
|
|
(cons x (keep f (rest s)))))))))
|
|
|
|
;; --- keep-indexed ---
|
|
(defn keep-indexed
|
|
([f]
|
|
(fn [rf]
|
|
(let [ia (volatile! -1)]
|
|
(fn ([] (rf)) ([result] (rf result))
|
|
([result input]
|
|
(let [i (vswap! ia inc)
|
|
v (f i input)]
|
|
(if (nil? v) result (rf result v))))))))
|
|
([f coll]
|
|
(letfn [(keepi [idx coll]
|
|
(lazy-seq
|
|
(when-let [s (seq coll)]
|
|
(let [x (f idx (first s))]
|
|
(if (nil? x)
|
|
(keepi (inc idx) (rest s))
|
|
(cons x (keepi (inc idx) (rest s))))))))]
|
|
(keepi 0 coll))))
|
|
|
|
;; --- map-indexed ---
|
|
(defn map-indexed
|
|
([f]
|
|
(fn [rf]
|
|
(let [i (volatile! -1)]
|
|
(fn ([] (rf)) ([result] (rf result))
|
|
([result input] (rf result (f (vswap! i inc) input)))))))
|
|
([f coll]
|
|
(letfn [(mapi [idx coll]
|
|
(lazy-seq
|
|
(when-let [s (seq coll)]
|
|
(cons (f idx (first s)) (mapi (inc idx) (rest s))))))]
|
|
(mapi 0 coll))))
|
|
|
|
;; --- cycle ---
|
|
(defn cycle [coll]
|
|
(if-let [vals (seq coll)]
|
|
(let [n (count vals)]
|
|
(letfn [(cstep [i]
|
|
(lazy-seq
|
|
(cons (nth vals (mod i n)) (cstep (inc i)))))]
|
|
(cstep 0)))
|
|
()))
|
|
|
|
;; repeatedly stays in the Janet seed for now (core-repeatedly): the canonical CLJ
|
|
;; version doesn't validate args, so (first (repeatedly non-fn)) / (repeatedly \a +)
|
|
;; don't throw like the stricter Janet version (repeatedly.cljc throw cases).
|
|
;; Ported separately once the non-fn / non-number-count throws are matched.
|
|
|
|
;; --- repeat ---
|
|
(defn repeat
|
|
([x] (lazy-seq (cons x (repeat x))))
|
|
([n x] (take n (repeat x))))
|
|
|
|
;; --- iterate ---
|
|
(defn iterate [f x]
|
|
(lazy-seq (cons x (iterate f (f x)))))
|
|
|
|
|
|
;; --- 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))))
|