I am interested in understanding the new language design of Python 3.x.
I do enjoy, in Python 2.7, the function map
:
Python 2.7.12
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
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 make 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
map
simply iterated over the result. Building a list when you don't need it is inefficient so the devs decided to makemap
lazy. There's a lot to be gained here for performance and not a lot to be lost (If you need a list, just ask for one ...list(map(...))
).