On Wed, Sep 29, 2010 at 7:06 PM, Paul Rubin <no.em...@nospam.invalid> wrote:
> As for the stdlib, the natural places for such a function would be > either itertools or functools, and the function should probably be called > "scan", inspired by this: > > http://en.wikibooks.org/wiki/Haskell/List_processing#Scans > > Python's version would be like "scanl" with an optional arg to make it > like "scanl1". > Something like this? NoInitialValue = object() def scan(function, iterable, initial=NoInitialValue): iterator = iter(iterable) if initial is NoInitialValue: try: accumulator = iterator.next() except StopIteration: return else: accumulator = initial yield accumulator for arg in iterator: accumulator = function(accumulator, arg) yield accumulator Cheers, Ian
-- http://mail.python.org/mailman/listinfo/python-list