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