From 89da5d7c3d9cf5bfd4a374e23fd924bbffdeaf5c Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 22 Aug 2006 00:21:25 +0000 Subject: Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M. --- Lib/csv.py | 12 +++---- Lib/difflib.py | 3 +- Lib/idlelib/AutoCompleteWindow.py | 6 ++-- Lib/idlelib/MultiCall.py | 22 ++++++------ Lib/test/test_builtin.py | 26 -------------- Lib/test/test_iter.py | 13 ------- Lib/test/test_random.py | 4 ++- Python/bltinmodule.c | 75 --------------------------------------- Tools/i18n/pygettext.py | 4 +-- Tools/pybench/pybench.py | 4 +-- 10 files changed, 25 insertions(+), 144 deletions(-) diff --git a/Lib/csv.py b/Lib/csv.py index f213854..79d72b6 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -221,12 +221,10 @@ class Sniffer: if m[n]: spaces += 1 - quotechar = reduce(lambda a, b, quotes = quotes: - (quotes[a] > quotes[b]) and a or b, quotes.keys()) + quotechar = max(quotes, key=quotes.get) if delims: - delim = reduce(lambda a, b, delims = delims: - (delims[a] > delims[b]) and a or b, delims.keys()) + delim = max(delims, key=delims.get) skipinitialspace = delims[delim] == spaces if delim == '\n': # most likely a file with a single column delim = '' @@ -285,14 +283,12 @@ class Sniffer: continue # get the mode of the frequencies if len(items) > 1: - modes[char] = reduce(lambda a, b: a[1] > b[1] and a or b, - items) + modes[char] = max(items, key=lambda x: x[1]) # adjust the mode - subtract the sum of all # other frequencies items.remove(modes[char]) modes[char] = (modes[char][0], modes[char][1] - - reduce(lambda a, b: (0, a[1] + b[1]), - items)[1]) + - sum(item[1] for item in items)) else: modes[char] = items[0] diff --git a/Lib/difflib.py b/Lib/difflib.py index 7ab682d..408079b 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -652,8 +652,7 @@ class SequenceMatcher: 1.0 """ - matches = reduce(lambda sum, triple: sum + triple[-1], - self.get_matching_blocks(), 0) + matches = sum(triple[-1] for triple in self.get_matching_blocks()) return _calculate_ratio(matches, len(self.a) + len(self.b)) def quick_ratio(self): diff --git a/Lib/idlelib/AutoCompleteWindow.py b/Lib/idlelib/AutoCompleteWindow.py index d8bbff4..8bed034 100644 --- a/Lib/idlelib/AutoCompleteWindow.py +++ b/Lib/idlelib/AutoCompleteWindow.py @@ -335,10 +335,8 @@ class AutoCompleteWindow: self.userwantswindow = True return - elif reduce(lambda x, y: x or y, - [keysym.find(s) != -1 for s in ("Shift", "Control", "Alt", - "Meta", "Command", "Option") - ]): + elif any(s in keysym for s in ("Shift", "Control", "Alt", + "Meta", "Command", "Option")): # A modifier key, so ignore return diff --git a/Lib/idlelib/MultiCall.py b/Lib/idlelib/MultiCall.py index 4f53115..1c6103a 100644 --- a/Lib/idlelib/MultiCall.py +++ b/Lib/idlelib/MultiCall.py @@ -106,24 +106,26 @@ class _SimpleBinder: # _state_subsets gives for each combination of modifiers, or *state*, # a list of the states which are a subset of it. This list is ordered by the # number of modifiers is the state - the most specific state comes first. +# XXX rewrite without overusing functional primitives :-) _states = range(1 << len(_modifiers)) -_state_names = [reduce(lambda x, y: x + y, - [_modifiers[i][0]+'-' for i in range(len(_modifiers)) - if (1 << i) & s], - "") +_state_names = [''.join(m[0]+'-' + for i, m in enumerate(_modifiers) + if (1 << i) & s) for s in _states] _state_subsets = map(lambda i: filter(lambda j: not (j & (~i)), _states), - _states) + _states) for l in _state_subsets: l.sort(lambda a, b, nummod = lambda x: len(filter(lambda i: (1<ob_refcnt > 1) { - Py_DECREF(args); - if ((args = PyTuple_New(2)) == NULL) - goto Fail; - } - - op2 = PyIter_Next(it); - if (op2 == NULL) { - if (PyErr_Occurred()) - goto Fail; - break; - } - - if (result == NULL) - result = op2; - else { - PyTuple_SetItem(args, 0, result); - PyTuple_SetItem(args, 1, op2); - if ((result = PyEval_CallObject(func, args)) == NULL) - goto Fail; - } - } - - Py_DECREF(args); - - if (result == NULL) - PyErr_SetString(PyExc_TypeError, - "reduce() of empty sequence with no initial value"); - - Py_DECREF(it); - return result; - -Fail: - Py_XDECREF(args); - Py_XDECREF(result); - Py_DECREF(it); - return NULL; -} - -PyDoc_STRVAR(reduce_doc, -"reduce(function, sequence[, initial]) -> value\n\ -\n\ -Apply a function of two arguments cumulatively to the items of a sequence,\n\ -from left to right, so as to reduce the sequence to a single value.\n\ -For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ -((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ -of the sequence in the calculation, and serves as a default when the\n\ -sequence is empty."); - - -static PyObject * builtin_reload(PyObject *self, PyObject *v) { return PyImport_ReloadModule(v); @@ -2071,7 +1997,6 @@ static PyMethodDef builtin_methods[] = { {"ord", builtin_ord, METH_O, ord_doc}, {"pow", builtin_pow, METH_VARARGS, pow_doc}, {"range", builtin_range, METH_VARARGS, range_doc}, - {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, {"reload", builtin_reload, METH_O, reload_doc}, {"repr", builtin_repr, METH_O, repr_doc}, {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index bb0dd35..eadd149 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -462,12 +462,10 @@ class TokenEater: rentries = reverse[rkey] rentries.sort() for k, v in rentries: - isdocstring = 0 # If the entry was gleaned out of a docstring, then add a # comment stating so. This is to aid translators who may wish # to skip translating some unimportant docstrings. - if reduce(operator.__add__, v.values()): - isdocstring = 1 + isdocstring = any(v.values()) # k is the message string, v is a dictionary-set of (filename, # lineno) tuples. We want to sort the entries in v first by # file name and then by line number. diff --git a/Tools/pybench/pybench.py b/Tools/pybench/pybench.py index 7d90ba1..370175a 100755 --- a/Tools/pybench/pybench.py +++ b/Tools/pybench/pybench.py @@ -370,7 +370,7 @@ class Test: if runs == 0: return 0.0, 0.0, 0.0, 0.0 min_time = min(self.times) - total_time = reduce(operator.add, self.times, 0.0) + total_time = sum(self.times) avg_time = total_time / float(runs) operation_avg = total_time / float(runs * self.rounds @@ -570,7 +570,7 @@ class Benchmark: if runs == 0: return 0.0, 0.0 min_time = min(self.roundtimes) - total_time = reduce(operator.add, self.roundtimes, 0.0) + total_time = sum(self.roundtimes) avg_time = total_time / float(runs) max_time = max(self.roundtimes) return (min_time, avg_time, max_time) -- cgit v0.12