Sean Devlin wrote: > If you're running "edge" Clojure, and not 1.0, I'd recommend writing > the tests in Clojure next, using the clojure.test namespace. I'd like to keep it 1.0 for now, so the tests are in clojure.contrib.test-is. But I'll make note, or add an alternative version for clojure.test. It's nice to see the test support being added to core.
Chouser wrote: > >> (defn cmpr [[val1 freq1] [val2 freq2]] >> (let [freq (compare freq2 freq1)] >> (if (not= freq 0) freq (compare val1 val2)))) > > When testing for equality with zero, it's more idiomatic and > sometimes faster to use 'zero?', so perhaps: > > (if-not (zero? freq) freq (compare val1 val2)) > Yes, that's better. >> (defn -orderByFreq [_ coll] >> (if (empty? coll) () (keys (sort cmpr (count-words coll))))) > > Do you need to return an empty seq? If not, how about: > > (when (seq coll) > (keys (sort cmpr (count-words coll)))) I believe this is a place where Java and Clojure differs a bit. In Java I'd expect an empty list back, but in Clojure I guess nil makes more sense? Compare how Map.keySet() in Java returns an emply set for an empty map, but (keys map) in Clojure returns nill for an empty map. So, for interoperability with Java, I belive an empty seq is the way to go, but in a Clojure-only implementation nil would be a better fit. But in that case I guess I could just do: (keys (sort cmpr (count-words coll))) Benjy wrote: > > I've been playing along at home for awhile now, but this example hit > close to home -- I've written variations on this code countless times > in perl, and figured I'd take a stab at clojuring it while also taking > advantage of clojure.contrib.seq-utils's very useful to the matter at > hand group-by. I'd understand if the OP doesn't want to use this code > due to dependency on non-core or if the variations on making it more > general obscure the original intent... It's great with an alternative solution, and the dependency to clojure.contrib is not a problem. [...] > PS. To the original poster, the use of destructuring binds for the > arguments to cmpr was rad -- it was something I overlooked when I was > doing this in my head over dinner and I was imagining a bunch of > inanity involving hash lookups and ifs for falling back to comparing > the keys as you'd have to do in other languages. Yes, it's nice. My first few iterations used (val e) and (key e) but destruction works nice here I think. > > > (use '[clojure.contrib.seq-utils :only (group-by)]) > > ;; is there a better option for this? > (defn false-if-zero [t] (if (= t 0) nil t)) > > ;; the body of this fn should probably be a macro that takes > ;; any number of comparisons and or-chain them correctly such that > ;; ties cascade to the next comparison and obviates the need for > ;; explicit calls to false-if-zero. Does it already exist? > (defn cmpr [[key-a val-a] [key-b val-b]] > (or (false-if-zero (compare val-b val-a)) > (compare key-a key-b))) > > (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)) ))) > '()) > ;; 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... Yes, as you write, it may not be super-pretty, but it is handy if you need to guard against nil, as in this case. Thank you all for your feedback! /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 -~----------~----~----~----~------~----~------~--~---