[Python-Dev] A `cogen' module [was: Re: PEP 218 (sets); moving set.py to Lib]
Wed, 28 Aug 2002 10:49:20 -0400
On Wed, Aug 28, 2002 at 10:27:30AM -0400, Guido van Rossum wrote:
> > Ahh... re-iterability again...
> > This is a good example of a function that *fails silently* for non
> > re-iterable arguments.
> This failure is hardly silent IMO: the results are totally bogus,
> which is a pretty good clue that something's wrong.
Sure, at the interactive prompt or very shallow code it is obvious.
Exceptions are noisy. Anything else is silent.
> > Slurping the tail into a list loses the lazy efficiency of this function.
> > One of the ways I've used this function is to scan combinations until a
> > condition is satisfied. The iteration is always terminated before reaching
> > the end. Reading ahead may waste computation and memory.
> I don't understand. The Cartesian product calculation has to iterate
> over the second argument many times (unless you have it iterate over
> the first argument many times). So a lazy argument won't work. Am I
> missing something?
Even if all the arguments are re-iterable containers the recursive call
produces a lazy generator object - the cartesian product of the tail. I
don't want to read it eagerly into a list.