I was typing up an answer, but Jason answered faster and better :) The only thing I have to add is that 'frequencies' is also in clojure core as of 1.2.
On Jan 6, 1:13 pm, Jason Wolfe <ja...@w01fe.com> wrote: > You're not capturing the output of the reduce anywhere; doseq is for > side-effects only. > > If you wrapped the doseq in a "(def dictionary ...)" it would work, > but this is not recommended. > Instead, you should either use nested reductions, or produce a simple > list of tokens first (simpler): > > (defn process-file [file-name] > (with-open [rdr (BufferedReader. (FileReader. file-name))] > (reduce #(assoc %1 %2 (inc (get %1 %2 1))) {} > (mapcat #(re-seq #"[a-z]+" (.toLowerCase %)) > (line-seq rdr))))) > > (def dictionary (process-file "src/SpellChecker.clj")) > > (Untested). Or, using clojure-contrib functions: > > (defn process-file [file-name] > (->> (clojure.contrib.duck-streams/read-lines file-name) > (mapcat #(re-seq #"[a-z]+" (.toLowerCase %))) > clojure.contrib.seq/frequencies)) > > (also untested). > > Cheers, Jason > > On Jan 6, 10:49 am, new2clojure <miguel.arre...@gmail.com> wrote: > > > > > > > > > Hi, > > > I am trying to store into a map the frequency of each [a-z]+ word in a > > file. When I run this code in the repl the resulting dictionary is > > empty. How should I adapt my code to get this functionality right?. > > > Thank you in advance > > > (import (java.io BufferedReader FileReader)) > > > (def dictionary {}) > > > (defn process-file [file-name] > > (with-open > > [rdr (BufferedReader. (FileReader. file-name))] > > (doseq > > [line (line-seq rdr)] > > (reduce #(assoc %1 %2 (inc (get %1 %2 1))) dictionary (re-seq > > #"[a-z]+" (.toLowerCase line)))))) > > > (process-file "src/SpellChecker.clj") -- 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