liveBook · Manning

Zipper update function

As explained in Clojure: The Essential Reference

A zipper update function functionally changes the node or structure represented at a zipper location.

A zipper update function changes a tree at the current location without mutating the original input. Navigate to a location first, then use `zip/replace` to supply a new node, `zip/edit` to transform the current node with a function, or `zip/remove` to delete it. Each operation returns a location, so navigation and editing can be composed. Call `zip/root` when finished to retrieve the resulting tree; to edit it again, create a new zipper. section 8.4.4

(-> vzip zip/down zip/rightmost (zip/replace :replaced) zip/up zip/node) ; #1
;; [[1 2] [3 4 [5 10 [11 12]]] :replaced]
(-> vzip zip/down zip/rightmost (zip/edit conj 15) zip/up zip/node) ; #2
;; [[1 2] [3 4 [5 10 [11 12]]] [13 14 15]]
(-> vzip zip/down zip/rightmost zip/remove zip/root) ; #3
;; [[1 2] [3 4 [5 10 [11 12]]]]

Compare three edits at the rightmost child of a vector zipper: replacement changes it to `:replaced`, editing appends `15`, and removal omits it from the resulting root. section 8.4.4

Use `zip/replace` when the new value is already known and `zip/edit` when it should be computed from the existing node. Unlike an update, `zip/remove` also changes where the zipper is focused; call `zip/root` to inspect the complete result. section 8.4.4

How Clojure: The Essential Reference covers it

How deep it goes
Explains replace, edit, and remove through results at a navigated vector location, including root retrieval after editing.
Examples
Shows replacing a vector child with a keyword, appending a number through edit, and removing that child.
What it assumes
Introduces zipper creation and location navigation before showing updates to Clojure collections.
What you can do after
Apply it via clojure.zip to replace, transform, or remove a focused node and retrieve the resulting tree.
How it connects
Connects zipper updates to tree traversal, zipper locations, functional changes, and clojure.walk.

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.