1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/__builtins__/iteration/functional.py Thu Feb 02 22:38:26 2017 +0100
1.3 @@ -0,0 +1,101 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Functional operations for iterators.
1.8 +
1.9 +Copyright (C) 2015, 2016, 2017 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 filter(function, sequence):
1.26 +
1.27 + """
1.28 + Apply 'function' to each element in 'sequence', returning a sequence of all
1.29 + elements for which the result of the function evaluated to a true value.
1.30 + """
1.31 +
1.32 + l = []
1.33 + for i in sequence:
1.34 + if function(i):
1.35 + l.append(i)
1.36 + return l
1.37 +
1.38 +def map(function, sequence):
1.39 +
1.40 + """
1.41 + Apply 'function' to each element of 'sequence' in turn, appending the result
1.42 + to a new sequence containing all results.
1.43 + """
1.44 +
1.45 + l = []
1.46 + for i in sequence:
1.47 + l.append(function(i))
1.48 + return l
1.49 +
1.50 +_reduce_default = object()
1.51 +
1.52 +def reduce(function, sequence, initial=_reduce_default):
1.53 +
1.54 + """
1.55 + Using 'function', reduce the given 'sequence' to a single result.
1.56 +
1.57 + With no 'initial' value specified, the first two elements in the 'sequence'
1.58 + are used with the function to produce an initial result. With an initial
1.59 + result available, a subsequent result is computed by using the initial
1.60 + result and the next element in the sequence with the function.
1.61 +
1.62 + All subsequent results are computed using the current result and the next
1.63 + available element with the function. This continues for all remaining
1.64 + elements until the end of the sequence is reached.
1.65 + """
1.66 +
1.67 + result = initial
1.68 +
1.69 + for i in sequence:
1.70 + if result is _reduce_default:
1.71 + result = i
1.72 + else:
1.73 + result = function(result, i)
1.74 +
1.75 + return result
1.76 +
1.77 +def zip(args):
1.78 +
1.79 + """
1.80 + Zip the given 'args' together, producing for each index position tuples
1.81 + containing the values for that position from each of the 'args'.
1.82 + """
1.83 +
1.84 + result = []
1.85 + pos = 0
1.86 +
1.87 + # Repeat until one of the arguments runs out of elements.
1.88 +
1.89 + while True:
1.90 + l = []
1.91 +
1.92 + # Visit each argument in turn, collecting elements in the given
1.93 + # position.
1.94 +
1.95 + for arg in args:
1.96 + try:
1.97 + l.append(arg[pos])
1.98 + except IndexError:
1.99 + return result
1.100 +
1.101 + result.append(tuple(l))
1.102 + pos += 1
1.103 +
1.104 +# vim: tabstop=4 expandtab shiftwidth=4