Re: unevaluated sequences of initially indefinite length and 'filter'

Guido.van.Rossum@cwi.nl
Thu, 24 Feb 1994 12:47:06 +0100

I dug out your mods for sequences of arbitrary length and they set me
thinking.

(1) Shouldn't map() and reduce() be treated similarly?

(2) A possibly even more efficient way of doing this, which might work
for 'for' loops as well, would be to never ask for the length at all
-- simply keep asking for the next element until it raises IndexError.

This would in some cases require extra code (e.g. like what you
discovered about needing to use additem) but sounds like the most
general solution and even somewhat elegant...

Enter dream mode...

If I have an object representing a non-linear array (like a
dictionary) then it could return as its keys() an indefinite-length
sequence which generated the keys on the fly. You could then write

for key in x.keys():
print x[key]

And we would have introduced generators in the language without any
major surgery!

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>