On Tue, 2007-09-25 at 10:41 +0100, Paul Rudin wrote: > Going off on a tangent a bit, but I was idly considering the absence > of itertools.ireduce the other day. A problem is that reduce gives a > single result, so it's not clear what ireduce would do (perhaps why > it's not there). One obvious candidate would be to give the whole > sequence of partial reductions. I'm not sure if this would be > generally useful, but it would give a succinct way to do the Fibonacci > sequence: > > itertools.ireduce(operator.add, itertools.count())
Let's try it: >>> def ireduce(op, iterable, partial=0): ... for nxt in iterable: ... partial = op(partial, nxt) ... yield partial ... >>> import operator >>> from itertools import islice, count >>> >>> for x in islice(ireduce(operator.add, count()), 0, 10): ... print x ... 0 1 3 6 10 15 21 28 36 45 That's not the Fibonacci sequence. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list