1.1 --- a/lib/__builtins__/iterable.py Wed Feb 01 17:42:42 2017 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,189 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -Iteration-related functions.
1.8 -
1.9 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.10 -
1.11 -This program is free software; you can redistribute it and/or modify it under
1.12 -the terms of the GNU General Public License as published by the Free Software
1.13 -Foundation; either version 3 of the License, or (at your option) any later
1.14 -version.
1.15 -
1.16 -This program is distributed in the hope that it will be useful, but WITHOUT
1.17 -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 -FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 -details.
1.20 -
1.21 -You should have received a copy of the GNU General Public License along with
1.22 -this program. If not, see <http://www.gnu.org/licenses/>.
1.23 -"""
1.24 -
1.25 -def all(iterable):
1.26 -
1.27 - "Return whether all of the elements provided by 'iterable' are true."
1.28 -
1.29 - for i in iterable:
1.30 - if not i:
1.31 - return False
1.32 -
1.33 - return True
1.34 -
1.35 -def any(iterable):
1.36 -
1.37 - "Return whether any of the elements provided by 'iterable' are true."
1.38 -
1.39 - for i in iterable:
1.40 - if i:
1.41 - return True
1.42 -
1.43 - return False
1.44 -
1.45 -def enumerate(iterable, start=0):
1.46 -
1.47 - """
1.48 - Iterate over 'iterable', obtaining items and combining them with position
1.49 - information, producing a sequence containing tuples of the form
1.50 - (position, item). The first position is indicated by 'start' (which is zero
1.51 - by default) and each subsequent positions is incremented from the one
1.52 - preceding it.
1.53 - """
1.54 -
1.55 - l = []
1.56 - pos = start
1.57 -
1.58 - for i in iterable:
1.59 - l.append((pos, i))
1.60 - pos += 1
1.61 -
1.62 - return l
1.63 -
1.64 -def filter(function, sequence):
1.65 -
1.66 - """
1.67 - Apply 'function' to each element in 'sequence', returning a sequence of all
1.68 - elements for which the result of the function evaluated to a true value.
1.69 - """
1.70 -
1.71 - l = []
1.72 - for i in sequence:
1.73 - if function(i):
1.74 - l.append(i)
1.75 - return l
1.76 -
1.77 -def iter(collection):
1.78 -
1.79 - "Implementation of iter without callable plus sentinel support."
1.80 -
1.81 - return collection.__iter__()
1.82 -
1.83 -def len(obj):
1.84 -
1.85 - "Implementation of len."
1.86 -
1.87 - return obj.__len__()
1.88 -
1.89 -def map(function, sequence):
1.90 -
1.91 - """
1.92 - Apply 'function' to each element of 'sequence' in turn, appending the result
1.93 - to a new sequence containing all results.
1.94 - """
1.95 -
1.96 - l = []
1.97 - for i in sequence:
1.98 - l.append(function(i))
1.99 - return l
1.100 -
1.101 -def max(args):
1.102 -
1.103 - "Implementation of max."
1.104 -
1.105 - highest = None
1.106 - for arg in args:
1.107 - if highest is None or arg > highest:
1.108 - highest = arg
1.109 - return highest
1.110 -
1.111 -def min(args):
1.112 -
1.113 - "Implementation of min."
1.114 -
1.115 - lowest = None
1.116 - for arg in args:
1.117 - if lowest is None or arg < lowest:
1.118 - lowest = arg
1.119 - return lowest
1.120 -
1.121 -_reduce_default = object()
1.122 -
1.123 -def reduce(function, sequence, initial=_reduce_default):
1.124 -
1.125 - """
1.126 - Using 'function', reduce the given 'sequence' to a single result.
1.127 -
1.128 - With no 'initial' value specified, the first two elements in the 'sequence'
1.129 - are used with the function to produce an initial result. With an initial
1.130 - result available, a subsequent result is computed by using the initial
1.131 - result and the next element in the sequence with the function.
1.132 -
1.133 - All subsequent results are computed using the current result and the next
1.134 - available element with the function. This continues for all remaining
1.135 - elements until the end of the sequence is reached.
1.136 - """
1.137 -
1.138 - result = initial
1.139 -
1.140 - for i in sequence:
1.141 - if result is _reduce_default:
1.142 - result = i
1.143 - else:
1.144 - result = function(result, i)
1.145 -
1.146 - return result
1.147 -
1.148 -def reversed(sequence):
1.149 -
1.150 - "Return a reversed version of the given 'sequence'."
1.151 -
1.152 - return sequence[::-1]
1.153 -
1.154 -def sorted(iterable, cmp=None, key=None, reverse=False): pass
1.155 -
1.156 -def sum(sequence, start=0):
1.157 -
1.158 - "Sum the elements in 'sequence', adding to any indicated 'start' value."
1.159 -
1.160 - total = start
1.161 - for i in sequence:
1.162 - total += i
1.163 - return total
1.164 -
1.165 -def zip(args):
1.166 -
1.167 - """
1.168 - Zip the given 'args' together, producing for each index position tuples
1.169 - containing the values for that position from each of the 'args'.
1.170 - """
1.171 -
1.172 - result = []
1.173 - pos = 0
1.174 -
1.175 - # Repeat until one of the arguments runs out of elements.
1.176 -
1.177 - while True:
1.178 - l = []
1.179 -
1.180 - # Visit each argument in turn, collecting elements in the given
1.181 - # position.
1.182 -
1.183 - for arg in args:
1.184 - try:
1.185 - l.append(arg[pos])
1.186 - except IndexError:
1.187 - return result
1.188 -
1.189 - result.append(tuple(l))
1.190 - pos += 1
1.191 -
1.192 -# vim: tabstop=4 expandtab shiftwidth=4