On Fri, Jul 10, 2009 at 3:04 PM, Benjamin Stewart<bstew...@gmail.com> wrote

[...]

> (defn count-hash-vals [coll]
> ;; apply hash-map mapcat was the first thing
> ;; I found that worked here... better options ++welcome.
>               (apply hash-map
>                      (mapcat #(let [[k v] %]
>                                 (list k (count v)))
>                              coll)))
>
> (defn orderByFreq [coll]
>  (or
>       (keys (sort cmpr
>              (count-hash-vals (group-by identity coll)) ))) ;; as an
> exercise for the reader, use this call to implement (majority? ...) or
> (plurality? ...) etc.
>       '())  ;; I don't think this OR short circuiting is idiomatic in
> clojure, but it sure is handy for "empty list if nil" scenarios.
>             ;; Of course, (empty-list-or-nil ...) reads nice enough...

Here is a version without the apply/hash-map/mapcat that uses a plain
map instead. The function no longer returns a map, so the (keys map)
call has to be replaced as well:

(defn count-hash-vals [coll]
  (map (fn [[k v]] [k (count v)]) coll))

(defn -orderByFreq [_ coll]
  (or
    (map first (sort cmpr
      (count-hash-vals (group-by identity coll))))
  '()))

/Patrik

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