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. Clojure!
    An Introduction to Zippers!
    !
    Clojure User Group Paris – Nov 2013!

    View Slide

  2. #whoami  
    Jérémie Grodziski
    Software Designer and Programmer
    @jgrodziski
    [email protected]
    blog.zenmodeler.com  
    www.redsen.fr/blog  

    View Slide

  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

    View Slide

  4. History

    View Slide

  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

    View Slide

  6. Zipper functions

    View Slide

  7. Zipper creation

    View Slide

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

    View Slide

  9. Zipper navigation

    View Slide

  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!)

    View Slide

  11. Zipper edition

    View Slide

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

    View Slide

  13. One last thing
    If you just want to walk a tree:
    use tree-seq then clojure.walk functions

    View Slide