On 28 Apr 2014, at 10:57 am, Carlo Zancanaro <carlozancan...@gmail.com> wrote:
> ... > 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))))) > This is the technique used in the old contrib/io.clj read-lines function, isn't it? That's handy to know. Still, Stuart is skeptical about it. https://groups.google.com/forum/#!topic/clojure/MkHB3SHrGZY > 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}))) > That's cute. > 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. And they don't use with-open. So I guess with-open is a bit of problem, and you're back to managing file closing by yourself, which is what with-open was trying to circumvent, wasn't it? Peter
signature.asc
Description: Message signed with OpenPGP using GPGMail