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

6c7fb4b051926511027cec4116cfe8cf?s=128

Jérémie Grodziski

November 21, 2013
Tweet

Transcript

  1. Clojure! An Introduction to Zippers! ! Clojure User Group Paris

    – Nov 2013!
  2. #whoami   Jérémie Grodziski Software Designer and Programmer @jgrodziski jeremie@grodziski.com

    blog.zenmodeler.com   www.redsen.fr/blog  
  3. 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
  4. History

  5. 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
  6. Zipper functions

  7. Zipper creation

  8. Example means [] 0 [] 1A 1B 1C [] 2A

    2B 2C [] ...etc
  9. Zipper navigation

  10. 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!)
  11. Zipper edition

  12. Under the hood •  Zippers stores its “implementation” in metadata

  13. One last thing If you just want to walk a

    tree: use tree-seq then clojure.walk functions