Re: Working with zippers and trees

2013-11-27 Thread Martin Clausen
Ah, sorry. You could use a map-tree instead. It is a bit more verbose, but I believe it does what you want. (defn map-tree [root] (z/zipper map? #(seq (:cs %)) (fn [node children] (assoc node :cs (and children (apply vector children

Re: Working with zippers and trees

2013-11-27 Thread dabd
Your map-tree is exactly like an xml-zip. The problem I find with it is that now branch? also returns true for leaf nodes: (def m (map-tree {:v 1 :cs [{:v 2} {:v 3 :cs [{:v 4} {:v 5}]}]})) (- m z/down) [{:v 2} {:l [], :pnodes [{:v 1, :cs [{:v 2} {:v 3, :cs [{:v 4} {:v 5}]}]}], :ppath nil, :r

Re: Working with zippers and trees

2013-11-27 Thread Martin Clausen
You are right, but why is this a problem? The zipper works as intended and if you need to detect leaf nodes, that kan be done by checking for the presence of a non-empty value of the children key. On Wed, Nov 27, 2013 at 2:18 PM, dabd dario.reh...@gmail.com wrote: Your map-tree is exactly like

Re: Working with zippers and trees

2013-11-27 Thread dabd
Shouldn't be a practical problem just wondering if it's correct given the zipper definition requirements. On Wednesday, November 27, 2013 3:40:36 PM UTC, martin_clausen wrote: You are right, but why is this a problem? The zipper works as intended and if you need to detect leaf nodes, that

Re: Working with zippers and trees

2013-11-26 Thread martin_clausen
With a nested vector tree you can use the built in vector-zip function to create your zipper. As to the editing, the source codehttps://github.com/clojure/clojure/blob/master/src/clj/clojure/zip.clj contains a very similar use-case, which can be adapted to something like the following: (let

Re: Working with zippers and trees

2013-11-26 Thread dabd
The built-in vector-zip will build a tree with a different structure than what I need. I want build a tree as described in the first post: the node value is the first element of the vector and the children the rest of the elements. zipper.core (loop [loc (tree-zipper [1 2 [3 4 5]])] (if

Re: Working with zippers and trees

2013-11-26 Thread martin_clausen
To use the zipper library you have to be able to provide branch? children and make-node functions that apply to your data structure. I don't see a way to provide a meaningful branch? or children function for the structure you describe. Vector? will not work as branch? as it will not return

Re: Working with zippers and trees

2013-11-26 Thread Carlo Zancanaro
On Tue, Nov 26, 2013 at 08:30:15PM -0800, martin_clausen wrote: To use the zipper library you have to be able to provide branch? children and make-node functions that apply to your data structure. I don't see a way to provide a meaningful branch? or children function for the structure you

Re: Working with zippers and trees

2013-11-26 Thread dabd
I'm not sure what you mean by not being able to provide a meaningful branch?. I would like to represent a tree such a this: 1 / \ 2 3 / \ 4 5 How can I achieve this using zippers?

Re: Working with zippers and trees

2013-11-26 Thread Carlo Zancanaro
Okay, now after actually reading the documentation I can come back with an actually correct response! You should only have to change one function: (defn make-node [n children] (vec (cons (first n) children))) Your issue was that your original `make-node` function didn't return a

Re: Working with zippers and trees

2013-11-26 Thread dabd
Thanks. Do you think the call to z/edit in my code could be simplified? To edit a branch node I have to use conj to build a node only to pass it to make-node which will break it apart again. On Wednesday, November 27, 2013 5:53:31 AM UTC, Carlo wrote: Okay, now after actually reading the

Re: Working with zippers and trees

2013-11-26 Thread Martin Clausen
Yes, for instance like this: (let [vz (z/vector-zip [1 [2] [3 [4 5]]])] (loop [loc vz] (if (z/end? loc) (z/root loc) (recur (z/next (if (and (integer? (z/node loc)) (odd? (z/node loc))) (z/replace loc (* 2 (z/node loc)))

Re: Working with zippers and trees

2013-11-26 Thread dabd
The problem is that your vector-zip is not representing the tree I pictured. On Wednesday, November 27, 2013 6:26:12 AM UTC, martin_clausen wrote: Yes, for instance like this: (let [vz (z/vector-zip [1 [2] [3 [4 5]]])] (loop [loc vz] (if (z/end? loc)