Created
August 26, 2011 07:16
-
-
Save sunilnandihalli/1172883 to your computer and use it in GitHub Desktop.
learning to use zippers..
This file contains 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
(def s {:l {:l {:val 4} | |
:r {:val 6} | |
:val 10} | |
:r {:val 20 | |
:l {:val 11 | |
:l {:val 3} | |
:r {:val 8}} | |
:r {:val 9 | |
:l {:val 2} | |
:r {:val 7}}} | |
:val 30}) | |
(def btree (z/zipper (constantly true) #(keep identity [(:l %) (:r %)]) | |
(fn [nd [x y & rest]] | |
(if-not rest (assoc nd :l x :r y) | |
(throw (Exception. "Invalid use of make-node in zipper")))) s)) | |
(def inced-btree | |
(let [myinc #(when % | |
(clojure.pprint/pprint [% (meta %)]) | |
(update-in % [:val] inc))] | |
(loop [loc btree] | |
(if (z/end? loc) (z/root btree) | |
(recur (-> (z/edit loc myinc) z/next)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment