Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Hussein B.
I see. I tried to add more nested elements into my original structure but now the output isn't correct. My question is: Is it possible to implement a generic algorithm with Zippers that could traverse as long as needed and update an item? Maybe I'm doing zippers wrong in this case. Thanks a

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Moe Aboulkheir
Hussein, On Mon, Aug 24, 2015 at 5:40 PM, Hussein B. wrote: > But now the updated children is using list notation, not vector. Is it ok or > it is for displaying purposes? The collection type is now different, as the example I gave uses "reverse" as the transform, which is a generic sequence f

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Hussein B.
The result now is as desired: ({"a" {"b" 1, "c" 2}, "children" [{"a" {"b" 3, "c" 4}, "children" []}]} {"a" {"b" 5, "c" 6}, "children" []} {"a" {"b" 7, "c" 8}, "children" ({"a" {"b" 10, "c" 10}, "children" []} {"a" {"b" 9, "c" 10}, "children" []})}) But now the updated children is using list not

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Hussein B.
Oh, I defined my zipper as: (def loz (z/zipper #(contains? % "children") #(get % "children") (fn [_ x ] x) {"children" z})) But is throwing an exception. This one works: (def loz (z/zipper #(get % "children") #(get % "children") (fn [_ x ] x) {"children" z})) On Monday, August 24, 2015

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Hussein B.
Hi Moi, Thanks a lot for your patience and help. I tried this so far: (defn edit-parents [editable? edit loc] (loop [loc loc] (if (z/end? loc) (z/root loc) (if (editable? (z/node loc)) (recur (-> loc z/up (z/edit edit) z/up z/next)) (recur (z/next loc)) (

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Moe Aboulkheir
Went off half-cocked there. The remainder: (edit-parents #(= 10 (get-in % ["a" "b"])) #(update % "children" reverse), data-zipper) Would have the effect of reversing the order of the children in each node which possesses a child having a "b" attribute set to 10. You could probably express

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Moe Aboulkheir
Hussein, I'm not super comfortable with zippers, and probably wouldn't use them for something like this - but I had a go. If you define a zipper like this: (z/zipper #(get % "children") #(get % "children") (fn [_ x ] x) {"children" data}) Note the top-level vector is being wrapped in a map, so

Re: Why I'm getting NPE with zipper/next?

2015-08-24 Thread Hussein B.
Hi Moe, I have this structure: [{"a" {"b" 1 "c" 2} "children" [{"a" {"b" 3 "c" 4} "children" []}]} {"a" {"b" 5 "c" 6} "children" []} {"a" {"b" 7 "c" 8} "children" [{"a" {"b" 9 "c" 10} "children" []} {"a" {"b" 10 "c" 10} "children" []}]}] That is only a sample, the actual data is big

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Moe Aboulkheir
Hussein, I don't get an NPE passing that to traverse, but nothing much interesting happens either. The top-level data structure (and the vectors within "children") aren't associative, and so don't pass the branch? test (contains? % "children"). You could certainly extend the zipper to cover both

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Hussein B.
Here is my zipper: (z/zipper #(contains? % "children") #(get % "children") (fn [_ c] c) s) On Friday, August 21, 2015 at 6:49:25 PM UTC+2, Moe Aboulkheir wrote: > > Hussein, > > How are you constructing your zipper, before passing it to traverse? > Note that clojure.zip doesn't work on arbitr

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Moe Aboulkheir
Hussein, How are you constructing your zipper, before passing it to traverse? Note that clojure.zip doesn't work on arbitrary data structures without being given some information about how to descend into/construct nodes, etc. - i.e. z/next expects a zipper, and your data structure isn't a zipper,

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Hussein B.
Hi, I changed println to z/node , this time I'm getting: NullPointerException clojure.zip/branch? (zip.clj:73) On Friday, August 21, 2015 at 5:56:10 PM UTC+2, Moe Aboulkheir wrote: > > Hussein, > > The println inside (recur) will return nil. > > Take care, > Moe > > On Fri, Aug 21, 2015 at

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Colin Yates
I recall a while ago running into this. I ended up with the following: (defn stop? "Returns true if there is no point continuing past the specified loc. loc may be null or (z/end?). This is only here because z/up returns nil rather than something (z/end? will work with)." [loc] (or (n

Re: Why I'm getting NPE with zipper/next?

2015-08-21 Thread Moe Aboulkheir
Hussein, The println inside (recur) will return nil. Take care, Moe On Fri, Aug 21, 2015 at 4:35 PM, Hussein B. wrote: > Hi, > > I have this structure: > > (def s [{"n" {"id" "a"} "d" 2 "children" [{"n" {"id" "c"} "d" 4 "children" > []}]} {"n" {"id" "b"} "d" 3 "children" []}]) > > > > And I wro

Why I'm getting NPE with zipper/next?

2015-08-21 Thread Hussein B.
Hi, I have this structure: (def s [{"n" {"id" "a"} "d" 2 "children" [{"n" {"id" "c"} "d" 4 "children" []}]} {"n" {"id" "b"} "d" 3 "children" []}]) And I wrote a function with zippers to traverse it: (defn traverse [col] (loop [z col] (if (= (z/next z) z) z (if (z/branch? z)