Hey, for produces a lazy sequence (as does flatten) which is hurting you here. You could wrap everything in a doall but I'd recommend using reduce since thats technically what you want here. I'd probably go for something like:
(defn filter-file [filename] (with-open [rdr (io/reader filename)] (reduce (fn [words line] (->> (str/split line #"\s+") (filter #(and (<= (count %) 9) (>= (count %) 4))) (set) (set/union words))) #{} (line-seq rdr)))) Wether you want a set or a vector is up to you, set seems more logical to me here. Didn't check how expensive set/union is, might be better to use concat with a final set (after reduce) if there are a lot of lines. Anyways, use reduce. ;) HTH, /thomas On Saturday, June 8, 2013 6:53:05 AM UTC+2, Steven Arnold wrote: > > Hi, I am trying to write a function to extract words from a file that are > four characters or more, and nine characters or less. Many words could > appear on a single line, so the implementation needs to combine the words > from all the lines. I wrote a function to do this, but I am getting an > error that the stream is closed. > > The function and error are below. The idea is that we iterate over the > lines in the file, splitting each line on whitespace and selecting those > that meet the length requirements, giving us a list of lists, which is then > flattened into a single list. > > Most examples online of 'with-open' use it in conjunction with 'doseq', > but I don't think doseq returns the value of expressions like for does. > > As a Clojure newb, I'd welcome any feedback, but in particular I'm > interested in what's going on with the closed stream. Any thoughts? > > (defn filter-file > [] > (with-open [rdr (reader "/Users/thoth/wordlist.txt")] > (flatten > (for > [line (line-seq rdr)] > (filter > (and #(<= (count %) 9) > #(>= (count %) 4)) > (split line #"\s+")))))) > > The error is below. The 'ha-ha' and 'splenetic' are the first couple > words in the wordlist. In case you're wondering, they come from > dictionary.com's previous words of the day. > > [ 09:39 PM (6) theorem:thoth ~/Source/clojure/test ] > lein run > (ha-ha splenetic Exception in thread "main" java.io.IOException: Stream > closed > [...lengthy traceback omitted, but the "for" line seems to be the one that > triggers the error] > > Thanks in advance! > steven > -- -- 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.