1.1 --- a/lib/__builtins__/iterable.py Sat Dec 10 19:05:06 2016 +0100
1.2 +++ b/lib/__builtins__/iterable.py Sat Dec 10 21:13:54 2016 +0100
1.3 @@ -98,7 +98,32 @@
1.4 lowest = arg
1.5 return lowest
1.6
1.7 -def reduce(function, sequence, initial=None): pass
1.8 +_reduce_default = object()
1.9 +
1.10 +def reduce(function, sequence, initial=_reduce_default):
1.11 +
1.12 + """
1.13 + Using 'function', reduce the given 'sequence' to a single result.
1.14 +
1.15 + With no 'initial' value specified, the first two elements in the 'sequence'
1.16 + are used with the function to produce an initial result. With an initial
1.17 + result available, a subsequent result is computed by using the initial
1.18 + result and the next element in the sequence with the function.
1.19 +
1.20 + All subsequent results are computed using the current result and the next
1.21 + available element with the function. This continues for all remaining
1.22 + elements until the end of the sequence is reached.
1.23 + """
1.24 +
1.25 + result = initial
1.26 +
1.27 + for i in sequence:
1.28 + if result is _reduce_default:
1.29 + result = i
1.30 + else:
1.31 + result = function(result, i)
1.32 +
1.33 + return result
1.34
1.35 def reversed(sequence):
1.36