Hi, Gerald Britton wrote:
The sieve is just one example. The basic idea is that for some infinite generator (even a very simple one) you want to cut it off after some point. As for the number of characters, I spelled lambda incorrectly (left out a b) and there should be a space after the colon to conform to design guides. So, actually the takewhile version is two characters longer, not counting "import itertools" of course!
the only usefull approach I could see is to enable slice syntax on generators which would make it possible to describe the exact or maximum lenght of results you want out of it. something like: >> g=(i for i in xrange(1000))[2:5] >> g.next() # wrapper would now step 2 times w/o yield and 1 with yield 2 >> g.next() 3 >> g.next() 4 >> g.next() Traceback (most recent call last): File "<interactive input>", line 1, in <module> StopIteration as expected - this could be included into itertools for now. Regards Tino
On Mon, Jan 19, 2009 at 11:44 AM, Daniel Stutzbach <dan...@stutzbachenterprises.com> wrote:On Mon, Jan 19, 2009 at 10:37 AM, Gerald Britton <gerald.brit...@gmail.com> wrote:prime = (p for p in sieve() while p < 1000) prime = takewhile(lamda p:p<1000, sieve())I'm pretty sure the extra cost of evaluating the lambda at each step is tiny compared to the cost of the sieve, so I don't you can make a convincing argument on performance. Also, you know the latter is actually fewer characters, right? :-) -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/tino%40wildenhain.de
smime.p7s
Description: S/MIME Cryptographic Signature
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com