Alex Osborne wrote:

> If the three output lists themselves are too large, I'd just explicitly 
> sum your units with reduce:
> 
> (reduce
>   (fn [counts data]
>     (let [type (record-type data)]
>       (assoc counts type (+ (units data)
>                             (get counts type 0)))))
>   {} (get-idata))
> 
> => {:sales 1233, :upgrades 17, :demos 42, nil 30}

Actually come to think of it, this sort of thing is common enough that 
you could pull out a 'reduce-by' function like this:

(defn reduce-by [grouper f val coll]
   (reduce
    (fn [m x]
      (let [group (grouper x)]
        (assoc m group (f (get m group val) x))))
    (sorted-map) coll))

Then group-by could be easily defined in terms of it:

(defn group-by [f coll]
   (reduce-by f conj [] coll))

And it makes your unit summing example:

(reduce-by record-type
            (fn [count data] (+ count (units data)))
            0 (get-idata))

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to