[Python-Dev] List comp. sci fi

Paul Prescod paul@prescod.net
Wed, 12 Jul 2000 07:06:29 -0500

There is this idea on the edge of my brain that has to get out. I'm
afraid of overloading the idea too much while people are still
uncomfortable with the basics but at the same time I think that it is
exciting that there could be more power in list comprehensions than pure
syntactic sugar.

What if list comprehensions were lazy? They would be objects that
generate the items one at a time. Does this kill two birds with one
stone? Or just conflate two separate ideas?


naturals=[for num in [0:sys.maxint]] # does not construct list!!
evens=[for num in naturals: if num%2==0: num ] # ditto!!

chunks=[for chunk in file.read( 2048 ): chunk] # ditto!!
databaseRecords=[for rec in SQL.Execute( "..." ): rec ] ditto!!

Let's presume that name bindings are lexical and cyclic garbage
collection works as we expect it to. Converting lazy behavior to eager
is easy:

chunkList=tuple( chunks )

I'm not up to it right now, but I'm sure that there is a nice way to
combine sre and lazy comprehensions to get objects that return "hits" in
a string one at a time.

How to implement this? Stackless Python. <duck>

As my friend Joy says: Go for the big one or stay at home.
 Paul Prescod - Not encumbered by corporate consensus
Simplicity does not precede complexity, but follows it. 
	- http://www.cs.yale.edu/~perlis-alan/quotes.html