我想聚合大型数据集以获得类似的东西
SELECT SUM(`profit`) as `profit`, `month` FROM `t` GROUP BY `month`
所以,我像这样修改了clojure的group-by函数
(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)))
这是用法:
(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}}
它可以工作,但也许还有另一种方法可以做到这一点,使用 clojure 标准库?