My version: (defn top-words [input-filename result-filename] (spit result-filename (apply str (map #(format "%s : %d\n" (first %) (second %)) (sort-by #(-(val %)) (reduce #(conj %1 { %2 (inc (%1 %2 0)) }) {} (map #(.toLowerCase %) (re-seq #"\w+" (slurp input-filename)))))))))
Mibu On Dec 25, 2:16 pm, Piotr 'Qertoip' Włodarek <qert...@gmail.com> wrote: > Given the input text file, the program should write to disk a ranking > of words sorted by frequency, like: > > the : 52483 > and : 32558 > of : 23477 > a : 22486 > to : 21993 > > My first implementation: > > (defn topwords [in-filepath, out-filepath] > (def words (.split (.toLowerCase (slurp in-filepath)) "\\s+")) > > (spit out-filepath > (apply str > (concat > (map (fn [pair] (format "%20s : %5d \r\n" (key pair) > (val pair))) > (sort-by #( -(val %) ) > (reduce > (fn [counted-words word] > ( assoc counted-words > word > (inc (get counted-words > word 0)) )) > {} > words))) > ["\r\n"])))) > > Somehow I feel it's far from optimal. Could you please advise and > improve? What is the best, idiomatic implementation of this simple > problem? > > regards, > Piotrek --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---