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 def reduce(function, sequence, initial=None): pass 102 103 def reversed(sequence): 104 105 "Return a reversed version of the given 'sequence'." 106 107 return sequence[::-1] 108 109 def sorted(iterable, cmp=None, key=None, reverse=False): pass 110 111 def sum(sequence, start=0): 112 113 "Sum the elements in 'sequence', adding to any indicated 'start' value." 114 115 total = start 116 for i in sequence: 117 total += i 118 return total 119 120 def zip(args): 121 122 """ 123 Zip the given 'args' together, producing for each index position tuples 124 containing the values for that position from each of the 'args'. 125 """ 126 127 result = [] 128 pos = 0 129 130 # Repeat until one of the arguments runs out of elements. 131 132 while True: 133 l = [] 134 135 # Visit each argument in turn, collecting elements in the given 136 # position. 137 138 for arg in args: 139 try: 140 l.append(arg[pos]) 141 except IndexError: 142 return result 143 144 result.append(tuple(l)) 145 pos += 1 146 147 # vim: tabstop=4 expandtab shiftwidth=4