Right, that'll work, but it is no longer lazy in the sense that it
will read the whole sequence into memory (a problem for me because my
sequences are 10s of GB long, compressed).
The feature I was trying to show is that the "yield" function allows
you to make *arbitrary* non-lazy code lazy. (not just for cleanup, but
for anything)
In this particular case, the producer thread will only read 1000
objects ahead before blocking (inside the yield function) and waiting
for the consumers to catch up. Won't blow up memory.
Also, in this example, with-open doesn't really work for producing
lazy sequences. As you pointed out you must read the whole file to
avoid closing too early. However, with 'yield' it works because the
body of the with-yielding has its own thread. The body of the with-
open doesn't finish until the file is completely read.