简体   繁体   中英

group-by with reduce in clojure

I want to aggregate large dataset to get something like

SELECT SUM(`profit`) as `profit`, `month` FROM `t` GROUP BY `month`

So, i modified clojure's group-by function like so

(defn group-reduce [f red coll]
  (persistent!
   (reduce
    (fn [ret x]
      (let [k (f x)]
        (assoc! ret k (red (get ret k) x))))
    (transient {}) coll)))

And here is usage:

(group-reduce :month (fn [s x]
                       (if s
                         (assoc s :profit (+ (:profit s) (:profit x)))
                         x))
              [{:month 10 :profit 12}
               {:month 10 :profit 15}
               {:month 12 :profit 1}])

#_=> {10 {:profit 27, :month 10}, 12 {:profit 1, :month 12}}

It works, but maybe there is another way to do this, using clojure standard library?

Closest in the core is merge-with :

(def t [{:month 10 :profit 12}
        {:month 10 :profit 15}
        {:month 12 :profit 1}])

(apply merge-with + (for [x t] {(:month x) (:profit x)}))
;=> {12 1, 10 27}

Some examples:

user=> (def groups (group-by :month [{:month 10 :profit 12}
  #_=>                               {:month 10 :profit 15}
  #_=>                               {:month 12 :profit 1}])
{10 [{:profit 12, :month 10} {:profit 15, :month 10}], 12 [{:profit 1, :month 12}]}

user=> (for [[k v] groups] {:month k :sum-profit (apply + (map :profit v))})
({:month 10, :sum-profit 27} {:month 12, :sum-profit 1})

user=> (into {} (for [[k v] groups] [k (apply + (map :profit v))]))
{10 27, 12 1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM