[Python-ideas] itertools has starmap but no starreduce?

Vitor Bosshard algorias at gmail.com
Fri Feb 12 17:54:34 CET 2010

2010/2/12 Andrey Fedorov <anfedorov at gmail.com>:
> Is that right, or am I missing something obvious?

The semantics are non-obvious, for one. I assume you mean that the
following lines would be equivalent:

starreduce(f, iterable, initial)
reduce(lambda x,y: f(x, *y), iterable, initial)

This is useful for the types of reduction where the result of the
function is one type, and the iterable is a sequence of args to that
function. For example:

reduce(lambda x,y: str.replace(x, *y), (('a', 'aa'), ('b', 'bb',)), some_string)

could be written as

starreduce(str.replace, (('a', 'aa'), ('b', 'bb',)), some_string)

It seems like a very narrow use-case, unless you mean something else.


More information about the Python-ideas mailing list