-
-
Save fr33m0nk/fad8449b50fddd41f635e8c9dbdca033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[net.cgrand.xforms :as x]) | |
(defn rollup [dimensions valfn] | |
(let [[dim & dims] (reverse dimensions)] | |
(reduce | |
(fn [xform dim] | |
(comp | |
(x/by-key dim xform) | |
(x/transjuxt | |
{:detail (x/into {}) | |
:total (comp x/vals (map :total) (x/reduce +))}))) | |
(comp (x/by-key dim (map valfn)) | |
(x/transjuxt | |
{:detail (x/into {}) | |
:total (comp x/vals (x/reduce +))})) | |
dims))) | |
=> (into {} (rollup [:continent :country] :population) | |
[{:continent "Europe" :country "France" :population 66} | |
{:continent "Europe" :country "Germany" :population 80} | |
{:continent "Europe" :country "Belarus" :population 9} | |
{:continent "North-America" :country "USA" :population 319} | |
{:continent "North-America" :country "Canada" :population 35}]) | |
{:detail | |
{"Europe" | |
{:detail {"France" 66, "Germany" 80, "Belarus" 9}, :total 155}, | |
"North-America" {:detail {"USA" 319, "Canada" 35}, :total 354}}, | |
:total 509} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment