Returns a zipper for nested vectors, given a root vector
(require '[clojure.zip :as zip]) (defn zip-map [f loc] " Map f over every node of the zipper. The function received has the form (f node-value loc), the node value and its location" (loop [z loc] (if (zip/end? z) (zip/root z) ; perhaps you can call zip/seq-zip or zip/vector-zip? (recur (zip/next (zip/edit z f z)))))) ;; Multiply by 100 every node in the tree user=> (zip-map (fn [n nx] (if (vector? n) n (* n 100) )) (zip/vector-zip '[5 [10 20 30] [1 2 3] ])) ;; Be careful! the returned result by zip/root is not a zipper anymore! [500 [1000 2000 3000] [100 200 300]]
;; It may not be obvious however it is worth pointing out that ;; this implementation assumes that all vectors are branches, ;; and all non vector data is therefore a leaf node. ;; A vector "Tree" [5 [10 20 30] [1 2 3]]) can be visualised as ;; ;; --- ♦ --- ;; / | \ ;; 5 ♦ ♦ ;; / | \ / | \ ;; 10 20 30 1 2 3 ;; If you want to put data into a branch node for example a binary tree ;; you can not use the default zip/vector-zip. Rather you need use zip/zipper ;; and give a new children function to specify where the children are. ;; See: https://stackoverflow.com/questions/26268098/can-someone-give-an-example-of-how-edit-function-for-clojure-zip-works
vector-zip