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