See my library at http://github.com/jpalmucci/clj-yield, which makes this trivial.
For example, here is a function I use to read a sequence of java serialized objects from a stream: (defn read-objects [path] (with-yielding [out 1000] (with-open [stream (java.io.ObjectInputStream. (java.io.BufferedInputStream. (java.util.zip.GZIPInputStream. (java.io.FileInputStream. (io/file path)))))] (loop [] (try (let [next (.readObject stream)] (yield out next) (recur)) (catch java.io.EOFException e (.close stream))))))) When the sequence returned by with-yielding becomes garbage collectable, yield will throw an exception causing with-open to close the file. Note that with-yielding will use a thread from the thread pool, so its a bad idea to have hundreds of active with-yieldings at once. On Aug 3, 3:21 pm, David Andrews <dammi...@gmail.com> wrote: > I want to create a lazy seq backed by an open file (or db connection, > or something else that needs cleanup). I can't wrap the consumer in a > with-anything. > > Is there a general method for cleaning up after the consumer discards > its reference to that lazy seq? I'm vaguely aware of Java finalize, > but am also aware that it is unpredictable (e.g. you aren't guaranteed > to be driven at the next gc). > > Does Clojure even provide the ability to define a finalize method for > a lazy seq? > > (Sipping water from a firehose...) -- 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