From: Raymond Hettinger Date: Thu, 2 Feb 2012 08:48:46 +0000 (-0800) Subject: Add pure python equivalent code for reduce(). X-Git-Tag: v2.7.3rc1~103 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d837a376999edaa127bb94df80a363515753f81;p=thirdparty%2FPython%2Fcpython.git Add pure python equivalent code for reduce(). --- diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 5be4edd61c19..3845f1960c46 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)