1.1 --- a/lib/__builtins__/iterable.py Sat Dec 10 18:54:07 2016 +0100
1.2 +++ b/lib/__builtins__/iterable.py Sat Dec 10 19:05:06 2016 +0100
1.3 @@ -21,8 +21,39 @@
1.4
1.5 def all(iterable): pass
1.6 def any(iterable): pass
1.7 -def enumerate(iterable): pass
1.8 -def filter(function, sequence): pass
1.9 +
1.10 +def enumerate(iterable, start=0):
1.11 +
1.12 + """
1.13 + Iterate over 'iterable', obtaining items and combining them with position
1.14 + information, producing a sequence containing tuples of the form
1.15 + (position, item). The first position is indicated by 'start' (which is zero
1.16 + by default) and each subsequent positions is incremented from the one
1.17 + preceding it.
1.18 + """
1.19 +
1.20 + l = []
1.21 + pos = start
1.22 +
1.23 + for i in iterable:
1.24 + l.append((pos, i))
1.25 + pos += 1
1.26 +
1.27 + return l
1.28 +
1.29 +def filter(function, sequence):
1.30 +
1.31 + """
1.32 + Apply 'function' to each element in 'sequence', returning a sequence of all
1.33 + elements for which the result of the function evaluated to a true value.
1.34 + """
1.35 +
1.36 + l = []
1.37 + for i in sequence:
1.38 + if function(i):
1.39 + l.append(i)
1.40 + return l
1.41 +
1.42 def iter(collection):
1.43
1.44 "Implementation of iter without callable plus sentinel support."
1.45 @@ -35,32 +66,56 @@
1.46
1.47 return obj.__len__()
1.48
1.49 -def map(function, *args): pass
1.50 +def map(function, sequence):
1.51 +
1.52 + """
1.53 + Apply 'function' to each element of 'sequence' in turn, appending the result
1.54 + to a new sequence containing all results.
1.55 + """
1.56
1.57 -def max(*args):
1.58 + l = []
1.59 + for i in sequence:
1.60 + l.append(function(i))
1.61 + return l
1.62 +
1.63 +def max(args):
1.64
1.65 "Implementation of max."
1.66
1.67 - highest = args[0]
1.68 - for arg in args[1:]:
1.69 - if arg > highest:
1.70 + highest = None
1.71 + for arg in args:
1.72 + if highest is None or arg > highest:
1.73 highest = arg
1.74 return highest
1.75
1.76 -def min(*args):
1.77 +def min(args):
1.78
1.79 "Implementation of min."
1.80
1.81 - lowest = args[0]
1.82 - for arg in args[1:]:
1.83 - if arg > lowest:
1.84 + lowest = None
1.85 + for arg in args:
1.86 + if lowest is None or arg < lowest:
1.87 lowest = arg
1.88 return lowest
1.89
1.90 def reduce(function, sequence, initial=None): pass
1.91 -def reversed(sequence): pass
1.92 +
1.93 +def reversed(sequence):
1.94 +
1.95 + "Return a reversed version of the given 'sequence'."
1.96 +
1.97 + return sequence[::-1]
1.98 +
1.99 def sorted(iterable, cmp=None, key=None, reverse=False): pass
1.100 -def sum(sequence, start=0): pass
1.101 +
1.102 +def sum(sequence, start=0):
1.103 +
1.104 + "Sum the elements in 'sequence', adding to any indicated 'start' value."
1.105 +
1.106 + total = start
1.107 + for i in sequence:
1.108 + total += i
1.109 + return total
1.110
1.111 def zip(args):
1.112