summaryrefslogtreecommitdiffstats
path: root/Doc/library/functions.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2012-02-02 08:48:46 (GMT)
committerRaymond Hettinger <python@rcn.com>2012-02-02 08:48:46 (GMT)
commit6d837a376999edaa127bb94df80a363515753f81 (patch)
treeb10b34e482b921b8e82b1799c986c68e21059d8d /Doc/library/functions.rst
parentc5f310c827c80ac29a3a883af84d07bef161013e (diff)
downloadcpython-6d837a376999edaa127bb94df80a363515753f81.zip
cpython-6d837a376999edaa127bb94df80a363515753f81.tar.gz
cpython-6d837a376999edaa127bb94df80a363515753f81.tar.bz2
Add pure python equivalent code for reduce().
Diffstat (limited to 'Doc/library/functions.rst')
-rw-r--r--Doc/library/functions.rst14
1 files changed, 13 insertions, 1 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 5be4edd..3845f19 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -1058,7 +1058,19 @@ available. They are listed here in alphabetical order.
it is placed before the items of the iterable in the calculation, and serves as
a default when the iterable is empty. If *initializer* is not given and
*iterable* contains only one item, the first item is returned.
-
+ Roughly equivalent to::
+
+ def reduce(function, iterable, initializer=None):
+ it = iter(iterable)
+ if initializer is None:
+ try:
+ initializer = next(it)
+ except StopIteration:
+ raise TypeError('reduce() of empty sequence with no initial value')
+ accum_value = initializer
+ for x in iterable:
+ accum_value = function(accum_value, x)
+ return accum_value
.. function:: reload(module)