I'm trying to understand a simple (as in other languages) workflow with clojure maps.
It basically comes down to this: How can chain these operations?
group-by
:key on a vector of mapsselect-keys
on remaining maps without the previous keygroup-by
again (0..n times) and select-keyscount
unique key instances at the end.
See also my previous question: Aggregate and Count in Maps
Example:
Given a vector of maps
(def DATA [{:a "X", :b "M", :c "K", :d 10}
{:a "Y", :b "M", :c "K", :d 20}
{:a "Y", :b "M", :c "F", :d 30}
{:a "Y", :b "P", :c "G", :d 40}])
performing group-by
(defn get-tree-level-1 [] (group-by :a DATA))
yields a map grouped by the value of that particular key.
{ X [{:a X, :b M, :c K, :d 10}],
Y [{:a Y, :b M, :c K, :d 20}
{:a Y, :b M, :c F, :d 30}
{:a Y, :b P, :c G, :d 40}]}
So far, so good. But what if I want to build a tree-like structure out of the data, which means selecting the remaining keys and ignoring some, select :b
and :c
and ignore :d
, which would yield in the next level:
(def DATA2 [{ :X [{:b "M", :c "K"}],
:Y [{:b "M", :c "K"}
{:b "M", :c "F"}
{:b "P", :c "G"}]}])
And finally, counting all instances of the remaining keys (e.g. count all unique values of the :b
key under the Y
-root):
(def DATA3 [{ :X [{:M 1}],
:Y [{:M 2}
{:P 1}])
I tried doing a select-keys
after the group-by
, but the result was empty after the first step:
(defn get-proc-sums []
(into {}
(map
(fn [ [k vs] ]
[k (select-keys vs [:b :c])])
(group-by :a DATA))))