Created
February 23, 2013 08:07
-
-
Save shark8me/5018917 to your computer and use it in GitHub Desktop.
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
| ;a sample zipper | |
| (def v2 (zip/vector-zip [[2 3] [4]])) | |
| (defn editfn [x] | |
| "returns a vector with 2 numbers" | |
| (let [a (* x 10)] | |
| [(inc a) (dec a)])) | |
| (defn inext [node-edit loc ] | |
| "Adds children if loc is a leaf, else returns the next node" | |
| (cond (zip/end? loc) loc | |
| (zip/branch? loc) (zip/next loc) | |
| :else (let [p (-> loc (zip/edit node-edit)) | |
| r (-> p zip/right)] | |
| (if (-> r nil? not) r | |
| (let [nextnode (-> p zip/up zip/right)] | |
| (if (-> nextnode nil? not) (-> nextnode zip/next) (-> p zip/down zip/rightmost zip/next ))))))) | |
| (defn leaf-grown [editfn v] | |
| "edits all the leaf nodes and returns after zipping to root" | |
| (zip/root (first (filter zip/end? (iterate (partial inext editfn) v))))) | |
| ;the leaf nodes 2 3 and 4 have been replaced with 2 nodes each. | |
| (is (= [[[21 19] [31 29]] [[41 39]]] (leaf-grown editfn v2))) | |
| ;the leaf nodes alone | |
| (is (= '(21 19 31 29 41 39) (map zip/node (leafnodes (-> (leaf-grown editfn v2) zip/vector-zip))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment