Hi,

On Aug 21, 12:05 pm, Jan Rychter <j...@rychter.com> wrote:

> It isn't what I want. But that's because I misspecified what I actually
> wanted. I didn't think about the problem enough. I need something more
> akin to a splice function:
>
> (splice tree1 index tree2)
>
> (splice '(1 2 (3 4 5) 6) 4 '(7 8 (9 10)))
> should produce => '(1 2 (3 7 8 (9 10) 6))

        (defn zip-splice
          [loc node new-node]
          (loop [loc loc]
            (cond
              (zip/end? loc)
              (throw (Exception. "Node not found"))

              (= (zip/node loc) node)
              (let [childs (zip/children new-node)
                    loc    (reduce #(zip/insert-left %1 %2) loc
childs)]
                (zip/remove loc))

              :else (recur (zip/next loc)))))

user=> (def z (zip/vector-zip [1 2 [3 4 5] 6]))
#'user/z
user=> (def s (zip/vector-zip [7 8 [9 10]]))
#'user/s
user=> (zip/root (zip-splice z 4 s))
[1 2 [3 7 8 [9 10] 5] 6]

Be aware of the '=' there. This you might want to replace with some
smarter function to check for the identity of a node. I stumpled over
that in this thread: 
http://groups.google.com/group/clojure/browse_thread/thread/bfd6539ec367a95b
I'm not really a tree expert. So if you have solved this problem,
please let me know. I'm still interested in this.

Sincerely
Meikel

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to