Skip to content

Commit a6c6037

Browse files
committed
Issues 2186 and 2187. Move map() from itertools to builtins.
1 parent 17301e9 commit a6c6037

File tree

8 files changed

+158
-203
lines changed

8 files changed

+158
-203
lines changed

Doc/library/itertools.rst

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ The tools are designed to combine readily with one another. This makes it easy
2222
to construct more specialized tools succinctly and efficiently in pure Python.
2323

2424
For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
25-
sequence ``f(0), f(1), ...``. This toolbox provides :func:`imap` and
26-
:func:`count` which can be combined to form ``imap(f, count())`` and produce an
27-
equivalent result.
25+
sequence ``f(0), f(1), ...``. But, this effect can be achieved in Python
26+
by combining :func:`map` and :func:`count` to form ``map(f, count())``.
2827

2928
Likewise, the functional tools are designed to work well with the high-speed
3029
functions provided by the :mod:`operator` module.
@@ -138,7 +137,7 @@ loops that truncate the stream.
138137
.. function:: count([n])
139138

140139
Make an iterator that returns consecutive integers starting with *n*. If not
141-
specified *n* defaults to zero. Often used as an argument to :func:`imap` to
140+
specified *n* defaults to zero. Often used as an argument to :func:`map` to
142141
generate consecutive data points. Also, used with :func:`izip` to add sequence
143142
numbers. Equivalent to::
144143

@@ -248,22 +247,6 @@ loops that truncate the stream.
248247
yield x
249248

250249

251-
.. function:: imap(function, *iterables)
252-
253-
Make an iterator that computes the function using arguments from each of the
254-
iterables. This function is the same as the built-in :func:`map` function.
255-
Equivalent to::
256-
257-
def imap(function, *iterables):
258-
iterables = [iter(it) for it in iterables)
259-
while True:
260-
args = [next(it) for it in iterables]
261-
if function is None:
262-
yield tuple(args)
263-
else:
264-
yield function(*args)
265-
266-
267250
.. function:: islice(iterable, [start,] stop [, step])
268251

269252
Make an iterator that returns selected elements from the iterable. If *start* is
@@ -421,7 +404,7 @@ loops that truncate the stream.
421404
.. function:: repeat(object[, times])
422405

423406
Make an iterator that returns *object* over and over again. Runs indefinitely
424-
unless the *times* argument is specified. Used as argument to :func:`imap` for
407+
unless the *times* argument is specified. Used as argument to :func:`map` for
425408
invariant parameters to the called function. Also used with :func:`izip` to
426409
create an invariant part of a tuple record. Equivalent to::
427410

@@ -437,9 +420,9 @@ loops that truncate the stream.
437420
.. function:: starmap(function, iterable)
438421

439422
Make an iterator that computes the function using arguments obtained from
440-
the iterable. Used instead of :func:`imap` when argument parameters are already
423+
the iterable. Used instead of :func:`map` when argument parameters are already
441424
grouped in tuples from a single iterable (the data has been "pre-zipped"). The
442-
difference between :func:`imap` and :func:`starmap` parallels the distinction
425+
difference between :func:`map` and :func:`starmap` parallels the distinction
443426
between ``function(a,b)`` and ``function(*c)``. Equivalent to::
444427

445428
def starmap(function, iterable):
@@ -507,7 +490,7 @@ can be combined. ::
507490
Check 1202 is for $823.14
508491

509492
>>> import operator
510-
>>> for cube in imap(operator.pow, range(1,5), repeat(3)):
493+
>>> for cube in map(operator.pow, range(1,5), repeat(3)):
511494
... print(cube)
512495
...
513496
1
@@ -577,7 +560,7 @@ which incur interpreter overhead. ::
577560

578561
def tabulate(function):
579562
"Return function(0), function(1), ..."
580-
return imap(function, count())
563+
return map(function, count())
581564

582565
def nth(iterable, n):
583566
"Returns the nth item or raise StopIteration"
@@ -603,7 +586,7 @@ which incur interpreter overhead. ::
603586

604587
def quantify(seq, pred=None):
605588
"Count how many times the predicate is true in the sequence"
606-
return sum(imap(pred, seq))
589+
return sum(map(pred, seq))
607590

608591
def padnone(seq):
609592
"""Returns the sequence elements and then returns None indefinitely.
@@ -617,7 +600,7 @@ which incur interpreter overhead. ::
617600
return chain.from_iterable(repeat(seq, n))
618601

619602
def dotproduct(vec1, vec2):
620-
return sum(imap(operator.mul, vec1, vec2))
603+
return sum(map(operator.mul, vec1, vec2))
621604

622605
def flatten(listOfLists):
623606
return list(chain.from_iterable(listOfLists))

Lib/filecmp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import os
1313
import stat
1414
import warnings
15-
from itertools import ifilterfalse, imap, izip
15+
from itertools import ifilterfalse, izip
1616

1717
__all__ = ["cmp","dircmp","cmpfiles"]
1818

@@ -130,8 +130,8 @@ def phase0(self): # Compare everything except common subdirectories
130130
self.right_list.sort()
131131

132132
def phase1(self): # Compute common names
133-
a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
134-
b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
133+
a = dict(izip(map(os.path.normcase, self.left_list), self.left_list))
134+
b = dict(izip(map(os.path.normcase, self.right_list), self.right_list))
135135
self.common = list(map(a.__getitem__, filter(b.__contains__, a)))
136136
self.left_only = list(map(a.__getitem__, ifilterfalse(b.__contains__, a)))
137137
self.right_only = list(map(b.__getitem__, ifilterfalse(a.__contains__, b)))

Lib/test/seq_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def __iter__(self):
7979
def __next__(self):
8080
raise StopIteration
8181

82-
from itertools import chain, imap
82+
from itertools import chain, map
8383
def itermulti(seqn):
8484
'Test multiple tiers of iterators'
85-
return chain(imap(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
85+
return chain(map(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
8686

8787
class CommonTest(unittest.TestCase):
8888
# The type to be tested

Lib/test/test_heapq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ def __iter__(self):
260260
def __next__(self):
261261
raise StopIteration
262262

263-
from itertools import chain, imap
263+
from itertools import chain, map
264264
def L(seqn):
265265
'Test multiple tiers of iterators'
266-
return chain(imap(lambda x:x, R(Ig(G(seqn)))))
266+
return chain(map(lambda x:x, R(Ig(G(seqn)))))
267267

268268
class TestErrorHandling(unittest.TestCase):
269269
# only for C implementation

Lib/test/test_itertools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
maxsize = test_support.MAX_Py_ssize_t
1010
minsize = -maxsize-1
1111
ifilter = filter
12+
imap = map
1213

1314
def lzip(*args):
1415
return list(zip(*args))

Lib/test/test_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,10 +1560,10 @@ def __iter__(self):
15601560
def __next__(self):
15611561
raise StopIteration
15621562

1563-
from itertools import chain, imap
1563+
from itertools import chain
15641564
def L(seqn):
15651565
'Test multiple tiers of iterators'
1566-
return chain(imap(lambda x:x, R(Ig(G(seqn)))))
1566+
return chain(map(lambda x:x, R(Ig(G(seqn)))))
15671567

15681568
class TestVariousIteratorArgs(unittest.TestCase):
15691569

Modules/itertoolsmodule.c

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,155 +1418,6 @@ static PyTypeObject starmap_type = {
14181418
};
14191419

14201420

1421-
/* imap object ************************************************************/
1422-
1423-
typedef struct {
1424-
PyObject_HEAD
1425-
PyObject *iters;
1426-
PyObject *func;
1427-
} imapobject;
1428-
1429-
static PyTypeObject imap_type;
1430-
1431-
static PyObject *
1432-
imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1433-
{
1434-
PyObject *it, *iters, *func;
1435-
imapobject *lz;
1436-
Py_ssize_t numargs, i;
1437-
1438-
if (type == &imap_type && !_PyArg_NoKeywords("imap()", kwds))
1439-
return NULL;
1440-
1441-
numargs = PyTuple_Size(args);
1442-
if (numargs < 2) {
1443-
PyErr_SetString(PyExc_TypeError,
1444-
"imap() must have at least two arguments.");
1445-
return NULL;
1446-
}
1447-
1448-
iters = PyTuple_New(numargs-1);
1449-
if (iters == NULL)
1450-
return NULL;
1451-
1452-
for (i=1 ; i<numargs ; i++) {
1453-
/* Get iterator. */
1454-
it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1455-
if (it == NULL) {
1456-
Py_DECREF(iters);
1457-
return NULL;
1458-
}
1459-
PyTuple_SET_ITEM(iters, i-1, it);
1460-
}
1461-
1462-
/* create imapobject structure */
1463-
lz = (imapobject *)type->tp_alloc(type, 0);
1464-
if (lz == NULL) {
1465-
Py_DECREF(iters);
1466-
return NULL;
1467-
}
1468-
lz->iters = iters;
1469-
func = PyTuple_GET_ITEM(args, 0);
1470-
Py_INCREF(func);
1471-
lz->func = func;
1472-
1473-
return (PyObject *)lz;
1474-
}
1475-
1476-
static void
1477-
imap_dealloc(imapobject *lz)
1478-
{
1479-
PyObject_GC_UnTrack(lz);
1480-
Py_XDECREF(lz->iters);
1481-
Py_XDECREF(lz->func);
1482-
Py_TYPE(lz)->tp_free(lz);
1483-
}
1484-
1485-
static int
1486-
imap_traverse(imapobject *lz, visitproc visit, void *arg)
1487-
{
1488-
Py_VISIT(lz->iters);
1489-
Py_VISIT(lz->func);
1490-
return 0;
1491-
}
1492-
1493-
static PyObject *
1494-
imap_next(imapobject *lz)
1495-
{
1496-
PyObject *val;
1497-
PyObject *argtuple;
1498-
PyObject *result;
1499-
Py_ssize_t numargs, i;
1500-
1501-
numargs = PyTuple_Size(lz->iters);
1502-
argtuple = PyTuple_New(numargs);
1503-
if (argtuple == NULL)
1504-
return NULL;
1505-
1506-
for (i=0 ; i<numargs ; i++) {
1507-
val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
1508-
if (val == NULL) {
1509-
Py_DECREF(argtuple);
1510-
return NULL;
1511-
}
1512-
PyTuple_SET_ITEM(argtuple, i, val);
1513-
}
1514-
result = PyObject_Call(lz->func, argtuple, NULL);
1515-
Py_DECREF(argtuple);
1516-
return result;
1517-
}
1518-
1519-
PyDoc_STRVAR(imap_doc,
1520-
"imap(func, *iterables) --> imap object\n\
1521-
\n\
1522-
Make an iterator that computes the function using arguments from\n\
1523-
each of the iterables. Stops when the shortest iterable is exhausted.");
1524-
1525-
static PyTypeObject imap_type = {
1526-
PyVarObject_HEAD_INIT(NULL, 0)
1527-
"itertools.imap", /* tp_name */
1528-
sizeof(imapobject), /* tp_basicsize */
1529-
0, /* tp_itemsize */
1530-
/* methods */
1531-
(destructor)imap_dealloc, /* tp_dealloc */
1532-
0, /* tp_print */
1533-
0, /* tp_getattr */
1534-
0, /* tp_setattr */
1535-
0, /* tp_compare */
1536-
0, /* tp_repr */
1537-
0, /* tp_as_number */
1538-
0, /* tp_as_sequence */
1539-
0, /* tp_as_mapping */
1540-
0, /* tp_hash */
1541-
0, /* tp_call */
1542-
0, /* tp_str */
1543-
PyObject_GenericGetAttr, /* tp_getattro */
1544-
0, /* tp_setattro */
1545-
0, /* tp_as_buffer */
1546-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1547-
Py_TPFLAGS_BASETYPE, /* tp_flags */
1548-
imap_doc, /* tp_doc */
1549-
(traverseproc)imap_traverse, /* tp_traverse */
1550-
0, /* tp_clear */
1551-
0, /* tp_richcompare */
1552-
0, /* tp_weaklistoffset */
1553-
PyObject_SelfIter, /* tp_iter */
1554-
(iternextfunc)imap_next, /* tp_iternext */
1555-
0, /* tp_methods */
1556-
0, /* tp_members */
1557-
0, /* tp_getset */
1558-
0, /* tp_base */
1559-
0, /* tp_dict */
1560-
0, /* tp_descr_get */
1561-
0, /* tp_descr_set */
1562-
0, /* tp_dictoffset */
1563-
0, /* tp_init */
1564-
0, /* tp_alloc */
1565-
imap_new, /* tp_new */
1566-
PyObject_GC_Del, /* tp_free */
1567-
};
1568-
1569-
15701421
/* chain object ************************************************************/
15711422

15721423
typedef struct {
@@ -3068,7 +2919,6 @@ izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
30682919
ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
30692920
islice(seq, [start,] stop [, step]) --> elements from\n\
30702921
seq[start:stop:step]\n\
3071-
imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
30722922
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
30732923
tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
30742924
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
@@ -3096,7 +2946,6 @@ inititertools(void)
30962946
&takewhile_type,
30972947
&islice_type,
30982948
&starmap_type,
3099-
&imap_type,
31002949
&chain_type,
31012950
&ifilterfalse_type,
31022951
&count_type,

0 commit comments

Comments
 (0)