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): 23 24 "Return whether all of the elements provided by 'iterable' are true." 25 26 for i in iterable: 27 if not i: 28 return False 29 30 return True 31 32 def any(iterable): 33 34 "Return whether any of the elements provided by 'iterable' are true." 35 36 for i in iterable: 37 if i: 38 return True 39 40 return False 41 42 def enumerate(iterable, start=0): 43 44 """ 45 Iterate over 'iterable', obtaining items and combining them with position 46 information, producing a sequence containing tuples of the form 47 (position, item). The first position is indicated by 'start' (which is zero 48 by default) and each subsequent positions is incremented from the one 49 preceding it. 50 """ 51 52 l = [] 53 pos = start 54 55 for i in iterable: 56 l.append((pos, i)) 57 pos += 1 58 59 return l 60 61 def filter(function, sequence): 62 63 """ 64 Apply 'function' to each element in 'sequence', returning a sequence of all 65 elements for which the result of the function evaluated to a true value. 66 """ 67 68 l = [] 69 for i in sequence: 70 if function(i): 71 l.append(i) 72 return l 73 74 def iter(collection): 75 76 "Implementation of iter without callable plus sentinel support." 77 78 return collection.__iter__() 79 80 def len(obj): 81 82 "Implementation of len." 83 84 return obj.__len__() 85 86 def map(function, sequence): 87 88 """ 89 Apply 'function' to each element of 'sequence' in turn, appending the result 90 to a new sequence containing all results. 91 """ 92 93 l = [] 94 for i in sequence: 95 l.append(function(i)) 96 return l 97 98 def max(args): 99 100 "Implementation of max." 101 102 highest = None 103 for arg in args: 104 if highest is None or arg > highest: 105 highest = arg 106 return highest 107 108 def min(args): 109 110 "Implementation of min." 111 112 lowest = None 113 for arg in args: 114 if lowest is None or arg < lowest: 115 lowest = arg 116 return lowest 117 118 _reduce_default = object() 119 120 def reduce(function, sequence, initial=_reduce_default): 121 122 """ 123 Using 'function', reduce the given 'sequence' to a single result. 124 125 With no 'initial' value specified, the first two elements in the 'sequence' 126 are used with the function to produce an initial result. With an initial 127 result available, a subsequent result is computed by using the initial 128 result and the next element in the sequence with the function. 129 130 All subsequent results are computed using the current result and the next 131 available element with the function. This continues for all remaining 132 elements until the end of the sequence is reached. 133 """ 134 135 result = initial 136 137 for i in sequence: 138 if result is _reduce_default: 139 result = i 140 else: 141 result = function(result, i) 142 143 return result 144 145 def reversed(sequence): 146 147 "Return a reversed version of the given 'sequence'." 148 149 return sequence[::-1] 150 151 def sorted(iterable, cmp=None, key=None, reverse=False): pass 152 153 def sum(sequence, start=0): 154 155 "Sum the elements in 'sequence', adding to any indicated 'start' value." 156 157 total = start 158 for i in sequence: 159 total += i 160 return total 161 162 def zip(args): 163 164 """ 165 Zip the given 'args' together, producing for each index position tuples 166 containing the values for that position from each of the 'args'. 167 """ 168 169 result = [] 170 pos = 0 171 172 # Repeat until one of the arguments runs out of elements. 173 174 while True: 175 l = [] 176 177 # Visit each argument in turn, collecting elements in the given 178 # position. 179 180 for arg in args: 181 try: 182 l.append(arg[pos]) 183 except IndexError: 184 return result 185 186 result.append(tuple(l)) 187 pos += 1 188 189 # vim: tabstop=4 expandtab shiftwidth=4