diff -r 4f9753139203 Doc/library/functions.rst --- a/Doc/library/functions.rst Tue Oct 08 21:29:27 2013 -0500 +++ b/Doc/library/functions.rst Wed Oct 09 11:56:49 2013 +0300 @@ -57,6 +57,11 @@ return False return True + Also equivalent to:: + + def all(iterable): + return functools.reduce(lambda x, y: x and y, iterable, True) + .. function:: any(iterable) @@ -69,6 +74,11 @@ return True return False + Also equivalent to:: + + def any(iterable): + return functools.reduce(lambda x, y: x or y, iterable, False) + .. function:: ascii(object) @@ -375,6 +385,11 @@ yield n, elem n += 1 + Also equivalent to:: + + def enumerate(iterable, start=0): + return zip(itertools.count(start), iterable) + .. function:: eval(expression, globals=None, locals=None) @@ -1303,6 +1318,12 @@ see :func:`math.fsum`\. To concatenate a series of iterables, consider using :func:`itertools.chain`. + If *iterable* returns numbers, :func:`sum` is equivalent to:: + + def sum(iterable, start=0): + return functools.reduce(operator.add, iterable, start) + + .. function:: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent or sibling diff -r 4f9753139203 Doc/library/functools.rst --- a/Doc/library/functools.rst Tue Oct 08 21:29:27 2013 -0500 +++ b/Doc/library/functools.rst Wed Oct 09 11:56:49 2013 +0300 @@ -198,13 +198,36 @@ Apply *function* of two arguments cumulatively to the items of *sequence*, from left to right, so as to reduce the sequence to a single value. For example, - ``reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``. + ``reduce(operator.add, [1, 2, 3, 4, 5])`` calculates ``((((1+2)+3)+4)+5)``. The left argument, *x*, is the accumulated value and the right argument, *y*, is the update value from the *sequence*. If the optional *initializer* is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If *initializer* is not given and *sequence* contains only one item, the first item is returned. + The three-argument version is equivalent to:: + + def reduce(function, iterable, initializer): + for element in iterable: + initializer = function(initializer, element) + return initializer + + while the two-argument version is equivalent to:: + + def reduce(function, iterable): + it = iter(iterable) + value = next(it) + for element in it: + value = function(value, element) + return value + + and also equivalent to:: + + def reduce(function, iterable): + for i in itertools.accumulate(iterable, function): + value = i + return value + .. decorator:: singledispatch(default) diff -r 4f9753139203 Doc/library/itertools.rst --- a/Doc/library/itertools.rst Tue Oct 08 21:29:27 2013 -0500 +++ b/Doc/library/itertools.rst Wed Oct 09 11:56:49 2013 +0300 @@ -135,6 +135,9 @@ '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32', '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60'] + See :func:`functools.reduce` for a similar function that returns only + the final accumulated value. + .. versionadded:: 3.2 .. versionchanged:: 3.3 @@ -348,6 +351,9 @@ if not predicate(x): yield x + See :func:`filter` for the complementary function that returns + elements of *iterable* for which *function* returns true. + .. function:: groupby(iterable, key=None)