]> granicus.if.org Git - python/commitdiff
Issue #19202: Add cross-reference and a rough code equivalent
authorRaymond Hettinger <python@rcn.com>
Sat, 12 Oct 2013 23:04:17 +0000 (16:04 -0700)
committerRaymond Hettinger <python@rcn.com>
Sat, 12 Oct 2013 23:04:17 +0000 (16:04 -0700)
Doc/library/functools.rst
Doc/library/itertools.rst

index d8fcf0f6cb3efc3c5b586f335bb572246b8d67fb..3c946e3eb28c96a0ec57e3c60f971a2903dc3bfe 100644 (file)
@@ -185,6 +185,18 @@ The :mod:`functools` module defines the following functions:
    a default when the sequence is empty.  If *initializer* is not given and
    *sequence* contains only one item, the first item is returned.
 
+   Equivalent to::
+
+      def reduce(function, iterable, initializer=None):
+          it = iter(iterable)
+          if initializer is None:
+              value = next(it)
+          else:
+              value = initializer
+          for element in it:
+              value = function(value, element)
+          return value
+
 
 .. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
 
index 25f34bff160a28390c58bc37e61045cdbd6cb42b..0156c05c67585ea113313ececfca50dfd74c9765 100644 (file)
@@ -135,6 +135,9 @@ loops that truncate the stream.
        '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