Skip to content

Commit 6480168

Browse files
committed
Issue #19202: Add cross-reference and a rough code equivalent
1 parent 5d4121a commit 6480168

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Doc/library/functools.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ The :mod:`functools` module defines the following functions:
185185
a default when the sequence is empty. If *initializer* is not given and
186186
*sequence* contains only one item, the first item is returned.
187187

188+
Equivalent to::
189+
190+
def reduce(function, iterable, initializer=None):
191+
it = iter(iterable)
192+
if initializer is None:
193+
value = next(it)
194+
else:
195+
value = initializer
196+
for element in it:
197+
value = function(value, element)
198+
return value
199+
188200

189201
.. function:: update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
190202

Doc/library/itertools.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ loops that truncate the stream.
135135
'0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
136136
'0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']
137137

138+
See :func:`functools.reduce` for a similar function that returns only the
139+
final accumulated value.
140+
138141
.. versionadded:: 3.2
139142

140143
.. versionchanged:: 3.3

0 commit comments

Comments
 (0)