delay and force in Python

Nick Coghlan ncoghlan at iinet.net.au
Mon Jan 24 06:22:32 EST 2005


Nick Coghlan wrote:
> Will Stuyvesant wrote:
> 
>> The program below creates a stream with the numbers 1..995
>> and then filters the stream, keeping only the even numbers,
>> and then prints the second number in the stream (implemented
>> as the first number of the tail, just like in the 3.5
>> Section in the Wizard book).
> 
> 
> How's this:
> 
> Py> from itertools import islice
> Py> print islice((x for x in xrange(1, 996) if x % 2 == 0), 1, 2).next()
> 4

Wouldn't it be nice if this could be spelt:

print (x for x in xrange(1, 996) if x % 2 == 0)[2]

Well, I just put a patch on SF to enable exactly that:
http://www.python.org/sf/1108272

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list