On Jun 14, 6:37 am, Wrexsoul <d2387...@bsnow.net> wrote: > On Jun 13, 11:07 pm, James Reeves <weavejes...@googlemail.com> wrote: > > > For instance, lets say I want to return a lazy list of all the lines > > in all the files in a directory tree: > > > (use '(clojure.contrib java-utils > > duck-streams)) > > When clojure.contrib releases version 1.0, that might be an option. >
Most interesting Clojure projects already depend on contrib in some form. You will need it eventually :) And frankly, the main reason there is not a 1.0 of contrib yet is that no-one has bothered to tag one. :P > > (defn all-line-seq [dir] > > (mapcat read-lines (file-seq (file dir)))) > > > Since file-seq, read-lines and mapcat all lazy, my final result is > > lazy as well. > > Seems to me that unless you completely consume the sequence, it will > leak a file handle. That's a problem with lazy seqs and IO in general. You just need to be careful. IIRC Rich is working on a solution, though. > > > Is file-seq recursive? I'd figured it just returned the flat contents > > > of a directory. > > > Yep, it's recursive. > > OK. Even so, the implementation in terms of super-lazy-seq shows the > power of that macro. Mostly, I'm doing quite a few things with I/O > that require custom next-item-in-seq generators for the records or > other objects returned, for which it's coming in real handy. In those > circumstances lazy-seq saves the bother of doing a loop/recur in > everyplace that consumes some records, and super-lazy-seq in turn lets > me still use a loop/recur style in the one place where the code > extracts and grovels over the records one by one. Maybe I'm just thick, but why would you want to use loop/recur style when the following works: (defn items-from-stateful-source [src] (when-let [item (take-item src)] ; take-item returns nil when there are no more items (lazy-seq (cons item (items-from-stateful-source src))))) Maybe I'm just too infatuated with higher-order functions and recursion. looping constructs are boring :) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---