# HG changeset patch # User Paul Boddie # Date 1481400834 -3600 # Node ID d75ff26f7c0aecf62eb0931261fde547cc105729 # Parent 2ab8235da0608202ee225b24dad1d23d30850fb8 Implemented the reduce function, adding some relevant tests. diff -r 2ab8235da060 -r d75ff26f7c0a lib/__builtins__/iterable.py --- a/lib/__builtins__/iterable.py Sat Dec 10 19:05:06 2016 +0100 +++ b/lib/__builtins__/iterable.py Sat Dec 10 21:13:54 2016 +0100 @@ -98,7 +98,32 @@ lowest = arg return lowest -def reduce(function, sequence, initial=None): pass +_reduce_default = object() + +def reduce(function, sequence, initial=_reduce_default): + + """ + Using 'function', reduce the given 'sequence' to a single result. + + With no 'initial' value specified, the first two elements in the 'sequence' + are used with the function to produce an initial result. With an initial + result available, a subsequent result is computed by using the initial + result and the next element in the sequence with the function. + + All subsequent results are computed using the current result and the next + available element with the function. This continues for all remaining + elements until the end of the sequence is reached. + """ + + result = initial + + for i in sequence: + if result is _reduce_default: + result = i + else: + result = function(result, i) + + return result def reversed(sequence): diff -r 2ab8235da060 -r d75ff26f7c0a tests/range.py --- a/tests/range.py Sat Dec 10 19:05:06 2016 +0100 +++ b/tests/range.py Sat Dec 10 21:13:54 2016 +0100 @@ -1,27 +1,61 @@ +# Obtain a list computed by range. + l = range(0, 10) -print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -print len(l) # 10 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +print len(l) # 10 + +# Test a descending xrange. x = xrange(0, -10, -2) -print x # __builtins__.span.xrange(0, -10, -2) -print len(x) # 5 +print x # __builtins__.span.xrange(0, -10, -2) +print len(x) # 5 for i in x: - print i # 0 - # -2 - # -4 - # -6 - # -8 + print i # 0 + # -2 + # -4 + # -6 + # -8 + +# Test an empty xrange. x = xrange(0, -10, 2) -print x # __builtins__.span.xrange(0, 0, 2) -print len(x) # 0 +print x # __builtins__.span.xrange(0, 0, 2) +print len(x) # 0 + +# Test a simple ascending xrange. y = xrange(4, 8) -print y # __builtins__.span.xrange(4, 8, 1) -print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)] -print sum(y) # 22 -print map(lambda x: x*2, y) # [8, 10, 12, 14] -print filter(lambda x: x%2, y) # [5, 7] -print max(y) # 7 -print min(y) # 4 +print y # __builtins__.span.xrange(4, 8, 1) + +# Test enumerate and sum. + +print enumerate(y) # [(0, 4), (1, 5), (2, 6), (3, 7)] +print sum(y) # 22 + +# Test map and filter using lambdas. + +print map(lambda x: x*2, y) # [8, 10, 12, 14] +print filter(lambda x: x%2, y) # [5, 7] + +# Test the limits of the range. + +print max(y) # 7 +print min(y) # 4 + +# Reproduce the sum function using reduce and a lambda. + +print reduce(lambda x, y: x+y, y) # 22 +print reduce(lambda x, y: x+y, y, 0) # 22 + +# Test a single element range. + +single = xrange(3, 5, 2) +print list(single) # [3] +print reduce(lambda x, y: x+y, single) # [3] + +# Test a double element range. + +double = xrange(3, 5, 1) +print list(double) # [3, 4] +print reduce(lambda x, y: x+y, double) # [7]