mirror of
https://github.com/python/cpython.git
synced 2025-07-17 08:15:19 +00:00
Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M.
This commit is contained in:
parent
6cefeb0e81
commit
89da5d7c3d
10 changed files with 25 additions and 144 deletions
|
@ -1597,80 +1597,6 @@ For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
|
|||
These are exactly the valid indices for a list of 4 elements.");
|
||||
|
||||
|
||||
static PyObject *
|
||||
builtin_reduce(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *seq, *func, *result = NULL, *it;
|
||||
|
||||
if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
|
||||
return NULL;
|
||||
if (result != NULL)
|
||||
Py_INCREF(result);
|
||||
|
||||
it = PyObject_GetIter(seq);
|
||||
if (it == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"reduce() arg 2 must support iteration");
|
||||
Py_XDECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((args = PyTuple_New(2)) == NULL)
|
||||
goto Fail;
|
||||
|
||||
for (;;) {
|
||||
PyObject *op2;
|
||||
|
||||
if (args->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)
|
||||
{
|
||||
|
@ -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},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue