summaryrefslogtreecommitdiffstats
path: root/Lib/functools.py
diff options
context:
space:
mode:
authormadman-bob <madman.bob@hotmail.co.uk>2018-10-25 14:02:10 (GMT)
committerVictor Stinner <vstinner@redhat.com>2018-10-25 14:02:10 (GMT)
commite25d5fc18e6c4b0062cd71b2eb1fd2d5eb5e2d3d (patch)
treea5114f0bbea703956e85a820eb7e78f312fba3d3 /Lib/functools.py
parent6279c1c5003cd94b5e04e568ce3df7c4e8f1eaa3 (diff)
downloadcpython-e25d5fc18e6c4b0062cd71b2eb1fd2d5eb5e2d3d.zip
cpython-e25d5fc18e6c4b0062cd71b2eb1fd2d5eb5e2d3d.tar.gz
cpython-e25d5fc18e6c4b0062cd71b2eb1fd2d5eb5e2d3d.tar.bz2
bpo-32321: Add pure Python fallback for functools.reduce (GH-8548)
Diffstat (limited to 'Lib/functools.py')
-rw-r--r--Lib/functools.py43
1 files changed, 39 insertions, 4 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index 51048f5..39a4af8 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -13,10 +13,6 @@ __all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial',
'partialmethod', 'singledispatch', 'singledispatchmethod']
-try:
- from _functools import reduce
-except ImportError:
- pass
from abc import get_cache_token
from collections import namedtuple
# import types, weakref # Deferred to single_dispatch()
@@ -227,6 +223,45 @@ except ImportError:
################################################################################
+### reduce() sequence to a single item
+################################################################################
+
+_initial_missing = object()
+
+def reduce(function, sequence, initial=_initial_missing):
+ """
+ reduce(function, sequence[, initial]) -> value
+
+ Apply a function of two arguments cumulatively to the items of a 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). If initial is present, it is placed before the items
+ of the sequence in the calculation, and serves as a default when the
+ sequence is empty.
+ """
+
+ it = iter(sequence)
+
+ if initial is _initial_missing:
+ try:
+ value = next(it)
+ except StopIteration:
+ raise TypeError("reduce() of empty sequence with no initial value") from None
+ else:
+ value = initial
+
+ for element in it:
+ value = function(value, element)
+
+ return value
+
+try:
+ from _functools import reduce
+except ImportError:
+ pass
+
+
+################################################################################
### partial() argument application
################################################################################