[Python-ideas] Rehabilating reduce (as "fold")

Ron Adam ron3200 at gmail.com
Fri Jul 12 21:49:55 CEST 2013



On 07/12/2013 01:01 AM, Nick Coghlan wrote:


> Efficiently merging a collection of iterables into a list would then just be:
>
>      data = fold(operator.iadd, [], iterables)
>
> I'd personally be in favour of the notion of also allowing strings as the
> first argument, so you could instead write:
>
>      data = fold("+=", [], iterables)
>
>
> This could also be introduced as an alternative API in functools.


How about if start was first... and could take a class?

      data = fold(class, op, iterables)

      data = fold(instance, op, iterables)




> (Independent of this idea, it would actually be nice if the operator module
> had a dictionary mapping from op symbols to names, like
> operator.by_symbol["+="] giving operator.iadd)


I'm guessing that this is how that might be used?

         name = operator.symbols['+=']
         op_method = start.__getattribute__(name)


Looks good to me. +1

Cheers,
    Ron









More information about the Python-ideas mailing list