diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-02-27 03:35:00 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-02-27 03:35:00 +0000 |
commit | 46823e1229a8e50e2adc0f88d2583c8bf88a4a2f (patch) | |
tree | 5d8c6b51a7184a45d1fb4845f4d9ae26241b6750 /src | |
parent | b8df9cb2a3b388a80fed68cd0a2934df69b55187 (diff) |
added tree-seq, file-seq, xml-seq
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.clj | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/boot.clj b/src/boot.clj index 052ccefa..1a24a72c 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -2027,3 +2027,35 @@ find-doc [re-string] #^{:doc "Prints documentation for the var named by varname"} doc [varname] `(print-doc (var ~varname))) + +(defn + #^{:doc "returns a lazy sequence of the nodes in a tree, via a depth-first walk. + branch? must be a fn of one arg that returns true if passed a node that can have children (but may not). + children must be a fn of one arg that returns a sequence of the children. Will only be called on nodes + for which branch? returns true. Root is the root node of the tree, must be a branch."} +tree-seq [branch? children root] + (let [walk (fn walk [nodes] + (when-first node nodes + (lazy-cons + node + (if (branch? node) + (lazy-cat (walk (children node)) + (walk (rest nodes))) + (walk (rest nodes))))))] + (lazy-cons root (walk (children root))))) + +(defn + #^{:doc "A tree seq on java.io.Files"} +file-seq [dir] + (tree-seq + (fn [#^java.io.File f] (. f (isDirectory))) + (fn [#^java.io.File d] (seq (. d (listFiles)))) + dir)) + +(defn + #^{:doc "A tree seq on the xml elements as per xml/parse"} +xml-seq [root] + (tree-seq + (complement string?) + (comp seq :content) + root)) |