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?
On Wednesday, November 27, 2013 4:30:15 AM UTC, 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 describe. Vector? will not work as branch? as it will not
> return true if passed the first element in a vector, and next will not work
> as it will not return the children if passed the first element of a vector.
>
> It looks to me like you don't get past the first element because the call
> to z/next fails both (and (branch? loc) (down loc)), (right loc) and (up
> loc) and therefore marks the first element as the end of the of the
> structure.
>
> Is there a compelling reason for not using the vector-zip structure for
> your specific use-case?
>
>
> On Wednesday, November 27, 2013 2:59:40 AM UTC+1, dabd wrote:
>>
>> 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 (z/end? loc)
>> (z/root loc)
>> (do (println (z/node loc))
>> (recur (z/next loc)))))
>> [1 2 [3 4 5]]
>>
>> 2
>>
>> [3 4 5]
>>
>> 4
>>
>> 5
>>
>> [1 2 [3 4 5]]
>> zipper.core> (loop [loc (z/vector-zip [1 2 [3 4 5]])]
>> (if (z/end? loc)
>> (z/root loc)
>> (do (println (z/node loc))
>> (recur (z/next loc)))))
>> [1 2 [3 4 5]]
>>
>> 1
>>
>> 2
>>
>> [3 4 5]
>>
>> 3
>>
>> 4
>>
>> 5
>>
>> [1 2 [3 4 5]]
>>
>>
>> On Tuesday, November 26, 2013 11:56:45 PM UTC, martin_clausen wrote:
>>>
>>> With a nested vector tree you can use the built in vector-zip function
>>> to create your zipper.
>>>
>>> As to the editing, the source
>>> code<https://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 [vz (vector-zip [1 2 [3 4 5]])]
>>> (loop [loc vz]
>>> (if (end? loc)
>>> (root loc)
>>> (recur (next (if (and (integer? (node loc)) (odd? (node
>>> loc)))
>>> (replace loc (* 2 (node loc)))
>>> loc))))))
>>>
>>> > [2 2 [6 4 10]]
>>>
>>> On Tuesday, November 26, 2013 10:50:34 PM UTC+1, dabd wrote:
>>>>
>>>> I am trying to work with a tree representation using vectors where the
>>>> first element is the node value and the rest are the children as suggested
>>>> here:
>>>>
>>>> http://grokbase.com/t/gg/clojure/12afy2cz9p/how-to-represent-trees-for-use-with-zippers
>>>>
>>>> However I am having trouble using clojure.zip/edit to change a simple
>>>> tree. I'd like to multiply the odd numbers by 2 in this case.
>>>> https://gist.github.com/dabd/7666778
>>>>
>>>> It looks like after editing the first (branch) node clojure.zip/next
>>>> will get to the end of the tree.
>>>> How can I correct this code?
>>>>
>>>> Thanks.
>>>>
>>>>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.