Slide 1

Slide 1 text

Clojure! An Introduction to Zippers! ! Clojure User Group Paris – Nov 2013!

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

History

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Zipper functions

Slide 7

Slide 7 text

Zipper creation

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Zipper navigation

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Zipper edition

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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