No, I'm sure to not use all the sequence, so I will follow your second advice, but...
Cause of my non-perfect english I've not really understand the last part. Who is the caller ? You suggest something like this: (let [fl (clojure.java.io/reader "path/filename") rd (lazy-reader fl)] (do-my-operation-with-lazy-reader) (.close fl)) ??? Or what ? On Dec 13, 1:20 am, Cedric Greevey <cgree...@gmail.com> wrote: > You also probably want more efficiency. Try something closer to: > > (defn lazy-reader [filename] > (let [rd (fn [rdr] > (let [buf (char-array 4096) > n (.read rdr buf 0 4096)] > (condp == n > -1 (.close rdr) > 0 (recur rdr) > (take n buf)))) > lr (fn lr [rdr] > (lazy-seq > (if-let [b (rd rdr)] > (concat b (lr rdr)))))] > (lr (clojure.java.io/reader filename)))) > > which should buffer the reads and terminate when the stream is > consumed (untested). I'm not sure how to additionally make the seq > "chunked". > > I avoided with-open to avoid the stream closing before the lazy seq is > consumed. Instead, it closes only when the lazy seq is fully consumed, > or when it's been discarded and the GC runs the stream's finalizer > after discovering that it's become unreachable. The latter could take > a while (and isn't guaranteed to happen short of the JVM exiting) so > there's a risk of running out of file handles using this a lot without > fully consuming the lazy seqs. If that could be an issue, I'd suggest > modifying it to take a reader rather than a filename, and making the > caller responsible for instantiating the reader, passing it in, > (partially) consuming the sequence, and closing the reader if the > sequence may not have been fully consumed. -- 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