ClojureDocs

Nav

Namespaces

vector-zip

clojure.zip

Available since 1.0
  • (vector-zip root)
Returns a zipper for nested vectors, given a root vector
2 Examples

(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
See Also

Returns a zipper for nested sequences, given a root sequence

Added by Chouser

Returns a zipper for xml elements (as from xml/parse), given a root element

Added by Chouser

Moves to the next loc in the hierarchy, depth-first. When reaching the end, returns a distinguishe...

Added by Arnoldo

Returns true if loc represents the end of a depth-first walk

Added by Arnoldo
0 Notes
No notes for vector-zip