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