[Python-Dev] A `cogen' module [was: Re: PEP 218 (sets); moving set.py to Lib]
Oren Tirosh
oren-py-d@hishome.net
Wed, 28 Aug 2002 12:10:36 -0400
On Wed, Aug 28, 2002 at 11:02:25AM -0400, Guido van Rossum wrote:
> > 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.
>
> And I wasn't proposing that.
>
> def cartesian(*sequences):
> if len(sequences) == 0:
> yield []
> else:
> head, tail = sequences[:-1], sequences[-1]
> tail = list(tail) # <--- This is what I was proposing
> for x in cartesian(*head):
> for y in tail:
> yield x + [y]
Silly me. Too much LISPthink made me automatically see "head" as the
first item and "tail" as the rest. Now I see that the head is all but the
last item and the tail is the last. It was really funny - like seeing the
cup change into two faces right in front of your eyes...
Oren