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 it resembles its
descendants.  And then define this utility function:

(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))))))

On Mon, Aug 24, 2015 at 10:40 AM, Hussein B. <hubaghd...@gmail.com> wrote:

> 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 bigger is properly more one or
> two nesting in the children attribute.
>
> I need to find a map by a criteria, say where b = 10 ({"a" {"b" 10 "c" 10}
> "children" []})
>
> Once it is found, I need to change its position within its parent. The
> parent is {"a" {"b" 7 "c" 8}
>
> I know that zippers are for editing data structures but I'm not sure how
> to do it.
>
> Thanks for help and time.
>
> On Friday, August 21, 2015 at 7:14:13 PM UTC+2, Moe Aboulkheir wrote:
>>
>> 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 cases, but there
>> may well be a more compact way to accomplish your goal.  What do you
>> want to do with the piece of data?
>>
>> Take care,
>> Moe
>>
>> On Fri, Aug 21, 2015 at 5:57 PM, Hussein B. <hubag...@gmail.com> wrote:
>> > 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 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, but an input to a zipper.  Unless
>> >> you've written a zipper you omitted from your post, zipping over maps,
>> >> in particular, may not be as convenient as you're imagining.
>> >> https://clojuredocs.org/clojure.zip/zipper has helpful examples in
>> it.
>> >>
>> >> Take care,
>> >> Moe
>> >>
>> >> On Fri, Aug 21, 2015 at 5:06 PM, Hussein B. <hubag...@gmail.com>
>> wrote:
>> >> > 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 4:35 PM, Hussein B. <hubag...@gmail.com>
>> 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 wrote a function with zippers to traverse it:
>> >> >> >
>> >> >> >  (defn traverse [col]
>> >> >> >   (loop [z col]
>> >> >> >     (if (= (z/next z) z)
>> >> >> >     z
>> >> >> >     (if (z/branch? z)
>> >> >> >       (recur (z/next z))
>> >> >> >       (recur (-> z println z/next))))))
>> >> >> >
>> >> >> >
>> >> >> > But I'm getting: NullPointerException   clojure.zip/next
>> >> >> > (zip.clj:236)
>> >> >> >
>> >> >> > Any ideas?
>> >> >> >
>> >> >> > Thanks for help.
>> >> >> >
>> >> >> > --
>> >> >> > You received this message because you are subscribed to the
>> Google
>> >> >> > Groups "Clojure" group.
>> >> >> > To post to this group, send email to clo...@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+u...@googlegroups.com
>> >> >> > 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 clojure+u...@googlegroups.com.
>> >> >> > For more options, visit https://groups.google.com/d/optout.
>> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups "Clojure" group.
>> >> > To post to this group, send email to clo...@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+u...@googlegroups.com
>> >> > 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 clojure+u...@googlegroups.com.
>> >> > For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Clojure" group.
>> > To post to this group, send email to clo...@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+u...@googlegroups.com
>> > 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 clojure+u...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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
> ---
> 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 clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to