Finishes a port the prior team started and reverted (bb4a3e0): the 40-lazy.clj
tier moved lazy seq fns Janet→Clojure but regressed the suite to 849 because
lazy-seq's expansion leaked as data in compile mode — that was jolt-r81, since
root-fixed (lazy-seq/lazy-cat moved to 00-syntax). With the wall gone, the port
works. This shrinks the Janet seed toward the north star (self-hosted
clojure-in-clojure on a minimal host bootstrap).
Moved to core/40-lazy.clj (wired as a loaded tier after 30-macros):
distinct keep keep-indexed map-indexed cycle repeat iterate
40-lazy.clj completed to full parity: distinct gains its transducer arity;
keep/keep-indexed/map-indexed already had both arities.
Removed from the Janet seed (core.janet): the 7 core-* fns + their core-bindings
entries, the now-dead td-keep/td-map-indexed transducer helpers (the CLJ versions
carry their own), and the already-dead core-partition-by/core-xml-seq (shadowed by
10-seq/20-coll). Net: core.janet −131 lines.
Deferred (kept in Janet, separate follow-ups): partition-all (a CLJ port via
take/drop realizes a non-minimal element count, tripping the §6.3 laziness
counters + a suite file) and repeatedly (canonical CLJ doesn't validate args, so
the repeatedly.cljc throw cases regress). Both need behavior-matching first.
Gate: conformance 262x3, lazy-infinite 44/44, clojure-test-suite 4004/66 (UP from
3981 — the CLJ versions add coverage, e.g. distinct value-equality), fixpoint,
self-host, sci 422/0, fallback-zero, specs+unit, core-bench all green.
106 lines
3.2 KiB
Clojure
106 lines
3.2 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 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.
|