liveBook · Manning

Zip/next

As explained in Clojure: The Essential Reference

zip/next is a Clojure zipper API function that advances a location along a depth-first traversal.

`zip/next` advances a zipper location through a depth-first traversal. It returns a location, not a node: use `zip/node` to read the data at that position. Unlike `clojure.walk`, a zipper lets traversal and processing be chosen separately. section 8.4.4

(-> vzip zip/next zip/node) ; #1
;; [1 2]

(-> vzip zip/next zip/next zip/node)
;; 1

(-> vzip zip/next zip/next zip/next zip/node)
;; 2

(-> vzip zip/next zip/next zip/next zip/next zip/node)
;; [3 4 [5 10 [11 12]]]

Successive calls visit the first child vector, then its elements `1` and `2`, then the next sibling vector. section 8.4.4

Walk and edit

`(->> z (iterate zip/next) (take-while (complement zip/end?)) (map zip/node))` collects visited nodes, including the starting node. To edit during traversal, apply an edit function to each location before calling `zip/next`; when `zip/end?` is true, `zip/root` retrieves the result. A walk that doubles leaves turns `[1 2 [3 4]]` into `[2 4 [6 8]]`. section 8.4.4

How Clojure: The Essential Reference covers it

How deep it goes
Shows successive visits, an iteration pattern, and an editing walk; identifies `zip/end?` as the stopping test.
Examples
Traces nested-vector visits and doubles leaves in a vector zipper.
What it assumes
Introduces the distinction between nodes and locations while using Clojure threading and sequence functions in examples.
What you can do after
Build it from scratch: write a zipper walk that advances, edits locations, checks for the end, and returns the root.
How it connects
Connects zipper locations with `zip/node`, `zip/end?`, `zip/root`, `zip/prev`, and `clojure.walk`.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.