Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure Zipper

Clojure Zipper

A lightning talk about the Zipper data structure at the Clojure User Group Paris (http://www.meetup.com/Paris-Clojure-User-Group/events/149254382/)
Source code for the live coding : https://gist.github.com/jgrodziski/7589471

Jérémie Grodziski

November 21, 2013
Tweet

More Decks by Jérémie Grodziski

Other Decks in Programming

Transcript

  1. What’s the problem? I have a tree I want to

    modify ONE node and don’t want to rebuild the whole tree 3 4 7 2 4 8 5 9 3 6 => Zipper
  2. Zipper fundamentals •  Purely functional data structure •  Think of

    it as a breacrumb or a zipper, that remember every direction you took from the root but starting from where you are, like an inverted glove
  3. loc, node and root A loc (location) point to the

    current node along with the context (up, left and right) think: « like a pointer » to a particular node in the tree (node loc) returns the node at the loc (root loc) returns the root node (and not the loc!)
  4. One last thing If you just want to walk a

    tree: use tree-seq then clojure.walk functions