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:
parent
d16e1f4eba
commit
97781b3ff0
1 changed files with 19 additions and 6 deletions
|
|
@ -164,20 +164,33 @@
|
|||
(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)]
|
||||
(if (branch? node)
|
||||
(reduce walk acc (children node))
|
||||
acc)))]
|
||||
(let [acc (conj acc node)]
|
||||
(if (branch? node)
|
||||
(reduce walk acc (children node))
|
||||
acc)))]
|
||||
(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
|
||||
([] ())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue