On 19 Aug 2015, at 18:08, Hussein B. <
hubag...@gmail.com> 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