Created
January 27, 2014 09:41
-
-
Save mattfield/8645759 to your computer and use it in GitHub Desktop.
Clojure tree walker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn tree-searcher | |
"Parse through every node in a tree and check against | |
give matcher function. If a match succeeds, return the node. | |
If no match is found after all recursions, just return the tree. | |
Also see clojure.walk" | |
[zipper matcher] | |
(loop [loc zipper] | |
(if (zip/end? loc) | |
(zip/root loc) | |
(if-let [matcher-result (matcher (zip/node loc))] | |
(matcher-result) | |
(recur (zip/next loc)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment