Clojureでマップのネストされたマップの特定のプロパティをカウントする慣用的な方法は何ですか?
次のデータ構造があるとします。
(def x {
:0 {:attrs {:attributes {:dontcare "something"
:1 {:attrs {:abc "some value"}}}}}
:1 {:attrs {:attributes {:dontcare "something"
:1 {:attrs {:abc "some value"}}}}}
:9 {:attrs {:attributes {:dontcare "something"
:5 {:attrs {:xyz "some value"}}}}}})
目的の出力を生成するにはどうすればよいですか:
(= (count-attributes x) {:abc 2, :xyz 1})
これはこれまでの私の最善の努力です:
(defn count-attributes
[input]
(let [result (for [[_ {{attributes :attributes} :attrs}] x
:let [v (into {} (remove (comp not :attrs) (vals attributes)))]]
(:attrs v))]
(frequencies result)))
これにより、次のものが生成されます。
{{:abc "some value"} 2, {:xyz "some value"} 1}