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@6 | 22 | def all(iterable): pass |
paul@6 | 23 | def any(iterable): pass |
paul@6 | 24 | def enumerate(iterable): pass |
paul@6 | 25 | def filter(function, sequence): pass |
paul@6 | 26 | def iter(collection): |
paul@6 | 27 | |
paul@6 | 28 | "Implementation of iter without callable plus sentinel support." |
paul@6 | 29 | |
paul@6 | 30 | return collection.__iter__() |
paul@6 | 31 | |
paul@6 | 32 | def len(obj): |
paul@6 | 33 | |
paul@6 | 34 | "Implementation of len." |
paul@6 | 35 | |
paul@6 | 36 | return obj.__len__() |
paul@6 | 37 | |
paul@6 | 38 | def map(function, *args): pass |
paul@6 | 39 | |
paul@6 | 40 | def max(*args): |
paul@6 | 41 | |
paul@6 | 42 | "Implementation of max." |
paul@6 | 43 | |
paul@6 | 44 | highest = args[0] |
paul@6 | 45 | for arg in args[1:]: |
paul@6 | 46 | if arg > highest: |
paul@6 | 47 | highest = arg |
paul@6 | 48 | return highest |
paul@6 | 49 | |
paul@6 | 50 | def min(*args): |
paul@6 | 51 | |
paul@6 | 52 | "Implementation of min." |
paul@6 | 53 | |
paul@6 | 54 | lowest = args[0] |
paul@6 | 55 | for arg in args[1:]: |
paul@6 | 56 | if arg > lowest: |
paul@6 | 57 | lowest = arg |
paul@6 | 58 | return lowest |
paul@6 | 59 | |
paul@6 | 60 | def reduce(function, sequence, initial=None): pass |
paul@6 | 61 | def reversed(sequence): pass |
paul@6 | 62 | def sorted(iterable, cmp=None, key=None, reverse=False): pass |
paul@6 | 63 | def sum(sequence, start=0): pass |
paul@286 | 64 | |
paul@286 | 65 | def zip(args): |
paul@286 | 66 | |
paul@286 | 67 | """ |
paul@286 | 68 | Zip the given 'args' together, producing for each index position tuples |
paul@286 | 69 | containing the values for that position from each of the 'args'. |
paul@286 | 70 | """ |
paul@286 | 71 | |
paul@286 | 72 | result = [] |
paul@286 | 73 | pos = 0 |
paul@286 | 74 | |
paul@286 | 75 | # Repeat until one of the arguments runs out of elements. |
paul@286 | 76 | |
paul@286 | 77 | while True: |
paul@286 | 78 | l = [] |
paul@286 | 79 | |
paul@286 | 80 | # Visit each argument in turn, collecting elements in the given |
paul@286 | 81 | # position. |
paul@286 | 82 | |
paul@286 | 83 | for arg in args: |
paul@286 | 84 | try: |
paul@286 | 85 | l.append(arg[pos]) |
paul@286 | 86 | except IndexError: |
paul@286 | 87 | return result |
paul@286 | 88 | |
paul@286 | 89 | result.append(tuple(l)) |
paul@286 | 90 | pos += 1 |
paul@6 | 91 | |
paul@6 | 92 | # vim: tabstop=4 expandtab shiftwidth=4 |