1 #!/usr/bin/env python 2 3 """ 4 Iteration-related functions. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 def all(iterable): pass 23 def any(iterable): pass 24 25 def enumerate(iterable, start=0): 26 27 """ 28 Iterate over 'iterable', obtaining items and combining them with position 29 information, producing a sequence containing tuples of the form 30 (position, item). The first position is indicated by 'start' (which is zero 31 by default) and each subsequent positions is incremented from the one 32 preceding it. 33 """ 34 35 l = [] 36 pos = start 37 38 for i in iterable: 39 l.append((pos, i)) 40 pos += 1 41 42 return l 43 44 def filter(function, sequence): 45 46 """ 47 Apply 'function' to each element in 'sequence', returning a sequence of all 48 elements for which the result of the function evaluated to a true value. 49 """ 50 51 l = [] 52 for i in sequence: 53 if function(i): 54 l.append(i) 55 return l 56 57 def iter(collection): 58 59 "Implementation of iter without callable plus sentinel support." 60 61 return collection.__iter__() 62 63 def len(obj): 64 65 "Implementation of len." 66 67 return obj.__len__() 68 69 def map(function, sequence): 70 71 """ 72 Apply 'function' to each element of 'sequence' in turn, appending the result 73 to a new sequence containing all results. 74 """ 75 76 l = [] 77 for i in sequence: 78 l.append(function(i)) 79 return l 80 81 def max(args): 82 83 "Implementation of max." 84 85 highest = None 86 for arg in args: 87 if highest is None or arg > highest: 88 highest = arg 89 return highest 90 91 def min(args): 92 93 "Implementation of min." 94 95 lowest = None 96 for arg in args: 97 if lowest is None or arg < lowest: 98 lowest = arg 99 return lowest 100 101 _reduce_default = object() 102 103 def reduce(function, sequence, initial=_reduce_default): 104 105 """ 106 Using 'function', reduce the given 'sequence' to a single result. 107 108 With no 'initial' value specified, the first two elements in the 'sequence' 109 are used with the function to produce an initial result. With an initial 110 result available, a subsequent result is computed by using the initial 111 result and the next element in the sequence with the function. 112 113 All subsequent results are computed using the current result and the next 114 available element with the function. This continues for all remaining 115 elements until the end of the sequence is reached. 116 """ 117 118 result = initial 119 120 for i in sequence: 121 if result is _reduce_default: 122 result = i 123 else: 124 result = function(result, i) 125 126 return result 127 128 def reversed(sequence): 129 130 "Return a reversed version of the given 'sequence'." 131 132 return sequence[::-1] 133 134 def sorted(iterable, cmp=None, key=None, reverse=False): pass 135 136 def sum(sequence, start=0): 137 138 "Sum the elements in 'sequence', adding to any indicated 'start' value." 139 140 total = start 141 for i in sequence: 142 total += i 143 return total 144 145 def zip(args): 146 147 """ 148 Zip the given 'args' together, producing for each index position tuples 149 containing the values for that position from each of the 'args'. 150 """ 151 152 result = [] 153 pos = 0 154 155 # Repeat until one of the arguments runs out of elements. 156 157 while True: 158 l = [] 159 160 # Visit each argument in turn, collecting elements in the given 161 # position. 162 163 for arg in args: 164 try: 165 l.append(arg[pos]) 166 except IndexError: 167 return result 168 169 result.append(tuple(l)) 170 pos += 1 171 172 # vim: tabstop=4 expandtab shiftwidth=4