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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.