开发者

GROUP BY and Aggregation on Vector of maps - Clojure

I have data that looks like this

(def a [{:firmAccount "MSFT" :Val 10  :PE 3 }  
        {:firmAccount "MSFT" :Val 15  :PE 4} 
        {:firmAccount "GOG" :Val 15 :PE 3} 
        {:firmAccount "YAH" :Val 8 :PE 1}])

I want to group by on :firmAccount and then SUM the :Val and :PE for each firm account and get something like

 [{:firmAccount "MSFT" :Val 25 :PE 7}
  {:firmAccount "GOG" :Val 15 :PE 3}    
  {:FirmAccount "YAH"开发者_高级运维 :Val 8 :PE 1}]

It is really a trivial thing and in SQL I would not even think twice but since I am learning clojure please bear with me


Clojure.core has a built-in group-by function. The solution becomes a little ugly by the presence of both text and int vals in the maps.

(for [m (group-by :firmAccount a)]
   (assoc (apply merge-with + (map #(dissoc % :firmAccount) (val m)))
          :firmAccount (key m)))


And for completeness here's an alternate version that uses map:

(map (fn [[firmAccount vals]] 
   {:firmAccount firmAccount 
    :Val (reduce + (map :Val vals)) 
    :PE (reduce + (map :PE vals))}) 
  (group-by :firmAccount a))


Try creating a new map array or map of maps with the same structure. You can write a function to add elements to this new map that sums that fields if the :firm-account exists. Maybe a map like this?

(def a {"MSFT" {:Val 25  :PE 7 }
        "GOG" {:Val 15 :PE 3} 
        "YAH" {:Val 8 :PE 1}})

With a personalized add function like:

(add-to-map [element map]
  (if (contains? (find-name element))
    {map (add-fields element (find (find-name element)))}
    {map element}))


It's a matter of style, but I find using thread-last macro (->>) is easier to read.

(->> a
     (group-by :firmAccount)
     (map (fn [[firmAccount vals]]
             {:firmAccount firmAccount 
              :Val (reduce + (map :Val vals)) 
              :PE (reduce + (map :PE vals))})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜