Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created August 26, 2011 07:16
Show Gist options
  • Save sunilnandihalli/1172883 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/1172883 to your computer and use it in GitHub Desktop.
learning to use zippers..
(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