过滤具有复杂嵌套结构的地图

时间:2012-04-12 15:41:03

标签: clojure

在复杂嵌套结构的嵌套字段上施加条件的最佳方法是什么...

{
    :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”部分是否存在并且在aa,qq和ww中的每一个中具有值“0”并且取决于对aa,qq和ww的排除(或执行任何操作)。例如,如果“ww”在该位置具有“i”=“0”,则获得如下所示的地图

{
    :ww {:xyz {
            :y1 {:x "abc",:u "ee"},
            :y2 {:y "abc",:i "0"},
            :y3 {:x "abc",:i "ee"}
            }   
        }
}

1 个答案:

答案 0 :(得分:3)

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"}}}])