Step 2e: tree-seq/xml-seq in Clojure overlay

xml-seq added to 20-coll.clj, matching Clojure reference:
  (tree-seq (complement string?) (comp seq :content) root)

tree-seq kept eager with lazy version documented in comments.
The lazy tree-seq (using lazy-seq + cons + mapcat) triggers
splice errors in self-hosted compilation mode — the self-hosted
compiler does not handle nested lazy-seq macros with recursive fn
bindings correctly. The mapcat overlay in 10-seq.clj has the same
limitation. This is a known compiler issue, not a design problem;
documented for future fix.

line-seq, iterator-seq, enumeration-seq remain Janet stubs —
Java-specific APIs with no Janet equivalent.

flatten already correct in Clojure overlay (unchanged).
This commit is contained in:
Yogthos 2026-06-08 08:47:23 -04:00
parent d16e1f4eba
commit 97781b3ff0

View file

@ -164,7 +164,16 @@
(let [a (f acc (first xs))] (recur a (next xs) (conj out a)))
out))))
;; Eager pre-order DFS (Clojure's is lazy; same order, fully realized here).
;; The lazy tree-seq (using lazy-seq/make-lazy-seq) correctly implements
;; Clojure semantics but triggers compile-mode issues in self-hosted compilation.
;; When compile mode is fixed, replace the eager version below with:
;; (defn tree-seq [branch? children root]
;; (let [walk (fn walk [node]
;; (lazy-seq
;; (cons node
;; (when (branch? node)
;; (mapcat walk (children node))))))]
;; (walk root)))
(defn tree-seq [branch? children root]
(let [walk (fn walk [acc node]
(let [acc (conj acc node)]
@ -174,10 +183,14 @@
(walk [] root)))
;; Canonical flatten via tree-seq: the leaves (non-sequential nodes) in order.
;; Flattens lists too (sequential?), which the prior Janet impl missed.
;; Flattens lists too (sequential?), matching Clojure/CLJS.
(defn flatten [coll]
(filter (complement sequential?) (rest (tree-seq sequential? seq coll))))
;; xml-seq: tree-seq over XML element trees. Elements are maps with :content.
(defn xml-seq [root]
(tree-seq (complement string?) (comp seq :content) root))
;; Lazy interleave: round-robin one element from each coll until any exhausts.
(defn interleave
([] ())