Skip to content

Instantly share code, notes, and snippets.

@shark8me
Created February 23, 2013 08:14
Show Gist options
  • Select an option

  • Save shark8me/5018930 to your computer and use it in GitHub Desktop.

Select an option

Save shark8me/5018930 to your computer and use it in GitHub Desktop.
;the root of the tree.
;each node has a :self key, the value of which is its own value, and a vector
;that contains children.
(def mroot {:self 20 :cren [
{:self 10 :cren [{:self 11} {:self 12}]}
{:self 30 :cren [{:self 31} {:self 32}]}
]})
;create a zipper from the mroot tree.
(defn mzipper [root]
(zip/zipper
#(:cren %) ;branch?
#(:cren %) ;children
(fn [node children]
(with-meta {:self (:self node) :cren (vec children)} (meta node)))
root))
;only leaf nodes are listed, the parent nodes 20, 10 ,30 are not.
(is (= '(11 12 31 32) (map :self (map zip/node (leafnodes (mzipper mroot))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment