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
2.1 --- a/tests/range.py Sat Dec 10 19:05:06 2016 +0100
2.2 +++ b/tests/range.py Sat Dec 10 21:13:54 2016 +0100
2.3 @@ -1,27 +1,61 @@
2.4 +# Obtain a list computed by range.
2.5 +
2.6 l = range(0, 10)
2.7 -print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.8 -print len(l) # 10
2.9 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.10 +print len(l) # 10
2.11 +
2.12 +# Test a descending xrange.
2.13
2.14 x = xrange(0, -10, -2)
2.15 -print x # __builtins__.span.xrange(0, -10, -2)
2.16 -print len(x) # 5
2.17 +print x # __builtins__.span.xrange(0, -10, -2)
2.18 +print len(x) # 5
2.19
2.20 for i in x:
2.21 - print i # 0
2.22 - # -2
2.23 - # -4
2.24 - # -6
2.25 - # -8
2.26 + print i # 0
2.27 + # -2
2.28 + # -4
2.29 + # -6
2.30 + # -8
2.31 +
2.32 +# Test an empty xrange.
2.33
2.34 x = xrange(0, -10, 2)
2.35 -print x # __builtins__.span.xrange(0, 0, 2)
2.36 -print len(x) # 0
2.37 +print x # __builtins__.span.xrange(0, 0, 2)
2.38 +print len(x) # 0
2.39 +
2.40 +# Test a simple ascending xrange.
2.41
2.42 y = xrange(4, 8)
2.43 -print y # __builtins__.span.xrange(4, 8, 1)
2.44 -print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)]
2.45 -print sum(y) # 22
2.46 -print map(lambda x: x*2, y) # [8, 10, 12, 14]
2.47 -print filter(lambda x: x%2, y) # [5, 7]
2.48 -print max(y) # 7
2.49 -print min(y) # 4
2.50 +print y # __builtins__.span.xrange(4, 8, 1)
2.51 +
2.52 +# Test enumerate and sum.
2.53 +
2.54 +print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)]
2.55 +print sum(y) # 22
2.56 +
2.57 +# Test map and filter using lambdas.
2.58 +
2.59 +print map(lambda x: x*2, y) # [8, 10, 12, 14]
2.60 +print filter(lambda x: x%2, y) # [5, 7]
2.61 +
2.62 +# Test the limits of the range.
2.63 +
2.64 +print max(y) # 7
2.65 +print min(y) # 4
2.66 +
2.67 +# Reproduce the sum function using reduce and a lambda.
2.68 +
2.69 +print reduce(lambda x, y: x+y, y) # 22
2.70 +print reduce(lambda x, y: x+y, y, 0) # 22
2.71 +
2.72 +# Test a single element range.
2.73 +
2.74 +single = xrange(3, 5, 2)
2.75 +print list(single) # [3]
2.76 +print reduce(lambda x, y: x+y, single) # [3]
2.77 +
2.78 +# Test a double element range.
2.79 +
2.80 +double = xrange(3, 5, 1)
2.81 +print list(double) # [3, 4]
2.82 +print reduce(lambda x, y: x+y, double) # [7]