clojure - Using `line-seq` with `reader`, when is the file closed? -



clojure - Using `line-seq` with `reader`, when is the file closed? -

i'm reading lines text file using (line-seq (reader "input.txt")). collection passed around , used program.

i'm concerned may bad style however, i'm not deterministically closing file. imagine can't utilize (with-open (line-seq (reader "input.txt"))), file stream potentially closed before i've traversed entire sequence.

should lazy-seq avoided in conjunction reader files? there different pattern should using here?

since doesn't have clear reply (it's mixed comments on first answer), here's essence of it:

(with-open [r (reader "input.txt")] (doall (line-seq r)))

that forcefulness whole sequence of lines read , close file. can pass result of whole look around.

when dealing big files, may have memory problems (holding whole sequence of lines in memory) , that's when it's thought invert program:

(with-open [r (reader "input.txt")] (doall (my-program (line-seq r))))

you may or may not need doall in case, depending on my-program returns and/or whether my-program consumes sequence lazily or not.

clojure io lazy-evaluation

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -