问题
Consider the following code
(def v (z/vector-zip [1 [2 [3 4]]]))
where z refers to clojure.zip.
Now, how do I create from v the vector
[1 [2 [3 [4 5]]]]
using functions from the API for clojure.zip ? So starting with
(->
v
...
回答1:
Just use function edit
(defn edit
"Replaces the node at this loc with the value of (f node args)"
[loc f & args]
(replace loc (apply f (node loc) args)))
Example
(-> v
(z/down)
(z/right)
(z/down)
(z/right)
(z/down)
(z/right)
(z/edit #(do [% 5]))
(z/root))
And the result will be
=> [1 [2 [3 [4 5]]]]
来源:https://stackoverflow.com/questions/37484870/how-to-add-a-child-to-a-tree-using-clojure-zip