[Python-ideas] Possible PEP 380 tweak

Peter Otten __peter__ at web.de
Tue Oct 26 10:12:54 CEST 2010


Guido van Rossum wrote:

>> I like your example because it matches the way I would have used
>> generators to solve it.  OTOH, it is not hard to rewrite parallel_reduce
>> as a traditional function.  In fact, the result is a bit shorter and
>> quite a bit faster so it is not a good example of what you need
>> generators for.
> 
> I'm not sure I understand. Maybe you meant to rewrite it as a class?
> There's some state that wouldn't have a good place to live without
> either a class or a (generator) stackframe to survive.

How about

def parallel_reduce(items, funcs):
    items = iter(items)
    try:
        first = next(items)
    except StopIteration:
        raise TypeError
    accu = [first] * len(funcs)
    for b in items:
        accu = [f(a, b) for f, a in zip(funcs, accu)]
    return accu

Peter




More information about the Python-ideas mailing list