Hi Dave,

Am 07.11.2012 um 20:09 schrieb Dave Ray:

> There aren't any problems with with-open/doseq/line-seq. The issue is
> with with-open/line-seq. For example, it's instinctive (at least for
> me anyway) to want to write a function like this:
> 
> (defn get-records [file-name]
>  (with-open [r (reader file-name)]
>    (line-seq r)))
> 
> Of course, the problem here is that the lazy sequence "escapes" the
> with-open scope and you get an exception when you start consuming it.
> And given a sequence there's no mechanism to say "I'm done with you,
> clean up any resources you may be using".
> 
> That's the crux of it for me anyway. It seems like the simplest
> approach is to just adjust my instincts and always make sure the
> sequence is fully consumed within the with-open.

You should probably adjust your instincts to the following API pattern:

(defn get-record-stream
  [stream]
  (line-seq stream))

(defn get-records
  [filename]
  (with-open [s (io/input-stream filename)]
    (doall (get-record-stream s))))

So you are free to choose what to do. Slurping the whole file, or handling the 
resource higher up the stack. Additionally you get non-file sources. Even if 
you normally don't need it, it comes in handy when testing.

My 0.02€.

Meikel

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