paul@528 | 1 | #!/usr/bin/env python |
paul@528 | 2 | |
paul@528 | 3 | """ |
paul@528 | 4 | Functional operations for iterators. |
paul@528 | 5 | |
paul@528 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@528 | 7 | |
paul@528 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@528 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@528 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@528 | 11 | version. |
paul@528 | 12 | |
paul@528 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@528 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@528 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@528 | 16 | details. |
paul@528 | 17 | |
paul@528 | 18 | You should have received a copy of the GNU General Public License along with |
paul@528 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@528 | 20 | """ |
paul@528 | 21 | |
paul@528 | 22 | def filter(function, sequence): |
paul@528 | 23 | |
paul@528 | 24 | """ |
paul@528 | 25 | Apply 'function' to each element in 'sequence', returning a sequence of all |
paul@528 | 26 | elements for which the result of the function evaluated to a true value. |
paul@528 | 27 | """ |
paul@528 | 28 | |
paul@528 | 29 | l = [] |
paul@528 | 30 | for i in sequence: |
paul@528 | 31 | if function(i): |
paul@528 | 32 | l.append(i) |
paul@528 | 33 | return l |
paul@528 | 34 | |
paul@528 | 35 | def map(function, sequence): |
paul@528 | 36 | |
paul@528 | 37 | """ |
paul@528 | 38 | Apply 'function' to each element of 'sequence' in turn, appending the result |
paul@528 | 39 | to a new sequence containing all results. |
paul@528 | 40 | """ |
paul@528 | 41 | |
paul@528 | 42 | l = [] |
paul@528 | 43 | for i in sequence: |
paul@528 | 44 | l.append(function(i)) |
paul@528 | 45 | return l |
paul@528 | 46 | |
paul@528 | 47 | _reduce_default = object() |
paul@528 | 48 | |
paul@528 | 49 | def reduce(function, sequence, initial=_reduce_default): |
paul@528 | 50 | |
paul@528 | 51 | """ |
paul@528 | 52 | Using 'function', reduce the given 'sequence' to a single result. |
paul@528 | 53 | |
paul@528 | 54 | With no 'initial' value specified, the first two elements in the 'sequence' |
paul@528 | 55 | are used with the function to produce an initial result. With an initial |
paul@528 | 56 | result available, a subsequent result is computed by using the initial |
paul@528 | 57 | result and the next element in the sequence with the function. |
paul@528 | 58 | |
paul@528 | 59 | All subsequent results are computed using the current result and the next |
paul@528 | 60 | available element with the function. This continues for all remaining |
paul@528 | 61 | elements until the end of the sequence is reached. |
paul@528 | 62 | """ |
paul@528 | 63 | |
paul@528 | 64 | result = initial |
paul@528 | 65 | |
paul@528 | 66 | for i in sequence: |
paul@528 | 67 | if result is _reduce_default: |
paul@528 | 68 | result = i |
paul@528 | 69 | else: |
paul@528 | 70 | result = function(result, i) |
paul@528 | 71 | |
paul@528 | 72 | return result |
paul@528 | 73 | |
paul@528 | 74 | def zip(args): |
paul@528 | 75 | |
paul@528 | 76 | """ |
paul@528 | 77 | Zip the given 'args' together, producing for each index position tuples |
paul@528 | 78 | containing the values for that position from each of the 'args'. |
paul@528 | 79 | """ |
paul@528 | 80 | |
paul@528 | 81 | result = [] |
paul@528 | 82 | pos = 0 |
paul@528 | 83 | |
paul@528 | 84 | # Repeat until one of the arguments runs out of elements. |
paul@528 | 85 | |
paul@528 | 86 | while True: |
paul@528 | 87 | l = [] |
paul@528 | 88 | |
paul@528 | 89 | # Visit each argument in turn, collecting elements in the given |
paul@528 | 90 | # position. |
paul@528 | 91 | |
paul@528 | 92 | for arg in args: |
paul@528 | 93 | try: |
paul@528 | 94 | l.append(arg[pos]) |
paul@528 | 95 | except IndexError: |
paul@528 | 96 | return result |
paul@528 | 97 | |
paul@528 | 98 | result.append(tuple(l)) |
paul@528 | 99 | pos += 1 |
paul@528 | 100 | |
paul@528 | 101 | # vim: tabstop=4 expandtab shiftwidth=4 |