简体   繁体   中英

How to add a child to a tree using clojure.zip?

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 
 ...

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]]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM