Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active May 22, 2020 20:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/01d4a5dec24acc912501c97bcaa1ceb7 to your computer and use it in GitHub Desktop.
Save cgrand/01d4a5dec24acc912501c97bcaa1ceb7 to your computer and use it in GitHub Desktop.
(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