Hussein,
Making this change to the zipper definition:
(z/zipper #(get % "children") #(get % "children") (fn [p c] (assoc p
"children" c)) {"children" z})
looks like it'll do the right thing here.
Take care,
Moe
On Thu, Aug 27, 2015 at 11:03 AM, Hussein B. <[email protected]> wrote:
> The modify function
>
> (defn modify [loc]
> (-> loc z/remove))
>
>
> On Thursday, August 27, 2015 at 11:58:31 AM UTC+2, Hussein B. wrote:
>>
>> Hi,
>>
>> I'm trying to remove an element from nested data structure (nesting in
>> unknown, so I'm trying to come up with a generic solution:
>>
>> (def z [
>> {"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" 11 "c" 12}
>> "children" [{"a" {"b" 13 "c" 14} "children" []}
>> {"a" {"b" 15 "c" 16} "children" []}]}]}])
>>
>>
>>
>>
>> (def loz (z/zipper #(get % "children") #(get % "children") (fn [_ x ] x)
>> {"children" z}))
>>
>>
>>
>> (defn to-change? [loc]
>> (if (= 11 (get-in (z/node loc) ["a" "b"]))
>> true
>> false))
>>
>>
>> (loop [loc loz]
>> (if (z/end? loc)
>> (z/root loc)
>> (recur (z/next
>> (cond (to-change? loc)
>> (modify loc)
>> :else loc)))))
>>
>>
>> That gives a very wrong output:
>>
>> ({"a" {"b" 1, "c" 2}, "children" [{"a" {"b" 3, "c" 4}, "children" []}]}
>> {"a" {"b" 5, "c" 6}, "children" []} ({"a" {"b" 9, "c" 10}, "children" []}))
>>
>>
>>
>> Parent {"a" {"b" 7 "c" 8} is getting deleted which is wrong.
>>
>> What I'm doing wrong?
>>
>> Thanks for help and time.
>
> --
> 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/d/optout.
--
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/d/optout.