On Mon, Apr 28, 2014 at 12:30:00AM +1000, Peter B. West wrote:
> I have no ambitions to solve the general problem; just to find a
> workable solution for my own use.

What is your own use? Your current solution involves using a shared
global variable to communicate between two functions, and tying up a
thread to wait to close a file handle. Both of these issues I consider
to be "unworkable".

There are other ways to manage resource scope, though. For instance, you
could have your "lazy-lines" function return a lazy-sequence which will
close the file on completely reading the stream:

  (defn lazy-lines [resource]
    (let [r ^java.io.Reader (vcf-res-reader resource)
          close #(.close r)]
      (concat (line-seq r)
              (lazy-seq (close)))))

Or one which provides you a function to close the stream when you want
to:

  (defn lazy-lines [resource]
    (let [r ^java.io.Reader (vcf-res-reader resource)
          close #(.close r)]
      (with-meta (line-seq r)
        {:close close})))

Or one which does both:

  (defn lazy-lines [resource]
    (let [r ^java.io.Reader (vcf-res-reader resource)
          close #(.close r)]
      (with-meta (concat (line-seq r)
                         (lazy-seq (close))) ;; close if we're done
        {:close close}))) ;; also provide the close function externally

None of these options mutate any global, shared memory. They all return
a sequence directly to the caller, and none of them require any sort of
coordination with other threads.

Attachment: signature.asc
Description: Digital signature

Reply via email to