Hi all,

Please have a look at the following function (explained in the doc
string). I have a bunch of rows that I need to aggregate using
different functions per field. I also need to apply the aggregate to a
subset of the rows. Obviously this is just like SQL aggregation, hence
the interface I've specified.

However I'm left wondering whether my implementation could be improved.

Particularly I very often find myself doing

(apply hash-map (flatten (for [[k v] some-map] ...)))

so often in fact that I feel like I'm missing something.

Any pointers or advice on improving my style will be much appreciated!


(defn aggregate
  "Aggregate rows using the functions provided in select and after
   filtering the rows by where.

    E.g.

      (aggregate [{:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 6}] :select {:a +}
:where (fn [r] (< (:b r) 6))) --> {:a 4}"

    [rows & {select :select where :where}]
    (letfn [(selected? [col] (some #{col} (keys select)))
             (agg [col] (get select col))]
      (apply hash-map
             (flatten
              (for [[k vs] (apply merge-with vector (filter where
rows)) :when (selected? k)]
                [k (apply (agg k) vs)])))))


Thanks,

-- 
David Jagoe

davidja...@gmail.com
+447535268218

-- 
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