Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a map with complex nested structure

Tags:

clojure

What would be a best way to impose a condition on the nested fields of complex nested structure like...

{
    :aa {:a "a_val",:b "b_val"},
    :qq {:abc 
            {
                :x1 {:x "abc",:u "ee"},
                :x2 {:y "abc",:i "ee"},
                :x3 {:x "abc",:i "ee"}
        }   
        },
    :ww {:xyz {
                :y1 {:x "abc",:u "ee"},
                :y2 {:y "abc",:i "0"},
                :y3 {:x "abc",:i "ee"}
              } 
        }
}

I want to check whether the "i" part exist and has value "0" in each of aa,qq and ww and depending upon that exclude(or perform any operation) on aa,qq and ww. For example if "ww" has "i"="0" at that position then get a map like below

{
    :ww {:xyz {
            :y1 {:x "abc",:u "ee"},
            :y2 {:y "abc",:i "0"},
            :y3 {:x "abc",:i "ee"}
            }   
        }
}
like image 564
Avi Avatar asked Apr 12 '12 15:04

Avi


1 Answers

user> (defn vvals [m] (when (map? m) (vals m)))
'user/vvals
user> (filter #(some #{"0"} (for [v (vvals (val %)), v (vvals v)] (:i v))) xx)
([:ww {:xyz {:y3 {:x "abc", :i "ee"}, :y2 {:y "abc", :i "0"}, :y1 {:x "abc", :u "ee"}}}])
like image 200
Marko Topolnik Avatar answered Oct 14 '22 01:10

Marko Topolnik