On 19 Aug 2015, at 18:08, Hussein B. <[email protected]> wrote:
> Here is more concrete example
>
> (def s [{"n" {"id" "a"} "d" 2 "children" [{"n" {"id" "c"} "d" 4 "children"
> nil}]} {"n" {"id" "b"} "d" 3 "children" nil}])
>
>
> I want to find the map that has value "c" for "id". If found, I need to
> return the map {"n" {"id" "c"} "d" 4 "children" nil}
One way to do this follows. First get all of the sub-maps, which you can do
with the following two functions:
(defn get-lower [x] (tree-seq coll? identity x))
(defn get-maps-from-lower [x] (filter map? (get-lower x))).
The first function gets all of the lower level colls, the second picks the maps
out of those colls.
You then need a function that will rummage round in a coll x looking to see if
it has the relevant element r:
(defn rummager [x r] (some #(= r %) x))
You then use rummager to get maps with the appropriate val
(defn get-map-with-val [v x]
(first (filter #(rummager (vals %) v) (get-maps-from-lower x)))).
Trying this out in the repl I get
=> (get-map-with-val {"id" "c"} s)
{"d" 4, "n" {"id" "c"}, "children" nil}
which I believe is the result you wanted.
Alan
--
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.