On Thu, Aug 16, 2012 at 5:47 PM, David Jacobs <da...@wit.io> wrote:
> I'm trying to grab 5 lines by their line numbers from a large (> 1GB) file
> with Clojure.
>
> So far I've got:
>
> (defn multi-nth [values indices]
>   (map (partial nth values) indices))
>
> (defn read-lines [file indices]
>   (with-open [rdr (clojure.java.io/reader file)]
>     (let [lines (line-seq rdr)]
>       (multi-nth lines indices))))
>
> Now, (read-lines "my-file" [0]) works without a problem. However, passing in
> [0 1] gives me the following error: "java.lang.RuntimeException:
> java.io.IOException: Stream closed"
>
> It seems that the stream is being closed before I can read the second line
> from the file. Interestingly, if I manually pull out a line from the file
> with something like `(nth lines 200)`, the `multi-nth` call works for all
> values <= 200.
>
> Any idea what's going on?

Laziness is biting you in this case, I imagine. You're not realizing
the result you seek until after with-open closes the file. You could
try throwing in a doall around the let perhaps. However, you probably
would want to change your algorithm a bit, as your code will hold on
the to the head of "lines" until all indices has been processed,
consuming memory for all lines read regardless of whether you are
interested in them. Also, nth with sequences is probably not what you
want most of the time.

Lars Nilsson

-- 
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

Reply via email to