NoIdeaHowToFixThis NoIdeaHowToFixThis - 1 month ago 8
Python Question

Ask why: Python 3.x map/

I am a little bit afraid asking with question (fear of down-votes) but I am somehow interested in understand the new language design of Python 3.x.

I do enjoy from time to time, in good old Python 2.7, the function ´map´.

Python 2.7.12 |Anaconda 4.0.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
In[2]: map(lambda x: x+1, [1,2,3])
Out[2]: [2, 3, 4]


However, in Python 3.x things have changed:

Python 3.5.1 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
In[2]: map(lambda x: x+1, [1,2,3])
Out[2]: <map at 0x4218390>


I understand the how, but I could not find a reference to the
why
. Why did the language designers made this choice, which, in my opinion, introduces a great deal of pain. Was this to arm-wrestle developers in sticking to list comprehensions.

IMO, list can be naturally thought as Functors; and I have been somehow been thought to think in this way:

fmap :: (a -> b) -> f a -> f b


Perhaps understanding the why would ease my pain.

Answer

I think the reason why map still exists at all when generator expressions also exist, is that it can take multiple iterator arguments that are all looped over and passed into the function:

>>> list(map(min, [1,2,3,4], [0,10,0,10]))
[0,2,0,4]

That's slightly easier than using zip:

>>> list(min(x, y) for x, y in zip([1,2,3,4], [0,10,0,10]))

Otherwise, it simply doesn't add anything over generator expressions.