liveBook · Manning

Zipper navigation function

As explained in Clojure: The Essential Reference

A zipper navigation function is a Clojure zipper API function that changes the focused location in a specified tree direction.

A zipper navigation function moves the focused location through a tree without prescribing a full traversal. Create a zipper with `zip/vector-zip`, `zip/seq-zip`, or `zip/xml-zip`; then use `zip/down`, `zip/up`, `zip/right`, or `zip/left` to move one step. section 8.4.4

Navigate a vector zipper

(def z (zip/vector-zip [[1 2] [3 4]])) (-> z zip/down zip/right zip/node) ;; [3 4] (-> z zip/down zip/down zip/down) ;; nil Navigation returns a location; `zip/node` retrieves its focused value. Moving down past a leaf returns `nil`. section 8.4.4

Use `zip/rightmost` or `zip/leftmost` to jump to an extreme sibling. For a built-in depth-first traversal instead, use `zip/next` or `zip/prev`. section 8.4.4

(pprint vzip)

;; [[[1 2] [3 4 [5 10 [11 12]]] [13 14]] ; #1
;;  nil]

(pprint (zip/down vzip))

;; [[1 2] ; #2
;;  {:l [],
;;   :pnodes [[[1 2] [3 4 [5 10 [11 12]]] [13 14]]],
;;   :ppath nil,
;;   :r ([3 4 [5 10 [11 12]]] [13 14])}]

(pprint (zip/rightmost (zip/down vzip)))
;; [[13 14] ; #3
;;  {:l [[1 2] [3 4 [5 10 [11 12]]]],
;;   :pnodes [[[1 2] [3 4 [5 10 [11 12]]] [13 14]]],
;;   :ppath nil,
;;   :r nil}]

Inspect how `zip/down` and `zip/rightmost` change the focused node and record its parent and siblings in the location. section 8.4.4

How Clojure: The Essential Reference covers it

How deep it goes
Defines directional moves and sibling jumps, then shows resulting locations and a failed descent past a leaf.
Examples
Shows vector, sequence, and XML zippers, including chained moves, sibling jumps, and node retrieval.
What it assumes
Assumes familiarity with Clojure collections while showing how to create zippers from vectors, sequences, or XML.
What you can do after
Apply it via `clojure.zip` to reach a chosen node or compose a partial tree traversal.
How it connects
Links zipper locations to node retrieval, tree editing, and depth-first traversal.

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.