将任意元数据附加到一个 zipping 可以是有用的。
这样做时,据我所知,除了达到末状态以外,所有返回 zipping 的 zipping 操作都会保留这些元数据。
我对以下代码进行了研究
(def res
(let [zloc (vary-meta (czip/vector-zip [1 2 3])
assoc :my-app/my-thing 42)
ops [["down" czip/down]
["up" czip/up]
["down" czip/down]
["right" czip/right]
["left" czip/left]
["rightmost" czip/rightmost]
["leftmost" czip/leftmost]
["next" czip/next]
["prev" czip/prev]
["up" czip/up]
["append-child" #(czip/append-child % "append-child")]
["down" czip/down]
["edit" #(czip/edit % str "-edit")]
["up" czip/up]
["insert-child" #(czip/insert-child % "insert-child")]
["down" czip/down]
["right" czip/right]
["insert-left" #(czip/insert-left % "insert-left")]
["insert-right" #(czip/insert-right % "insert-right")]
["rightmost" czip/rightmost]
["remove" czip/remove]
["replace" #(czip/replace % "replace")]
["rightmost" czip/rightmost]
["next past last node" czip/next]]]
(->> (reductions (fn [zloc [_desc f]] (f zloc))
zloc
ops)
(map vector (into [["--" "--"]] ops))
(map (fn [[[desc _f] zloc]]
{:op-desc desc
:end? (czip/end? zloc)
:node (czip/node zloc)
:root (czip/root zloc)
:my-meta (some-> zloc meta :my-app/my-thing)
:meta (meta zloc)})))))
如果我再搜索结果
(filter #(or (:end %)
(nil? (:my-meta %))
(nil? (:meta %)))
res)
;; => ({:op-desc "next past last node",
;; :end? true,
;; :node ["insert-child" "insert-left" "1-edit" "insert-right" 2 "replace"],
;; :root ["insert-child" "insert-left" "1-edit" "insert-right" 2 "replace"],
;; :my-meta nil,
;; :meta nil})
正如您所看到的,所有操作都保留了除返回的 zipping 处于末状态以外的一切任意元数据。
在上面的完整结果中没有多少价值,但让我们看一下任意单个结果,仅为了展示当我们不在 zipping 末状态时的样子
(nth res 11)
;; => {:op-desc "append-child",
;; :end? false,
;; :node [1 2 3 "append-child"],
;; :root [1 2 3 "append-child"],
;; :my-meta 42,
;; :meta
;; {:zip/branch? #function[clojure.core/vector?--5431],
;; :zip/children #function[clojure.core/seq--5419],
;; :zip/make-node #function[clojure.zip/vector-zip/fn--9351],
;; :my-app/my-thing 42}}