[Python-ideas] Make map() better

Edward Minnix egregius313 at gmail.com
Wed Sep 13 11:23:53 EDT 2017


While I agree that the method calling syntax is nicer, I disagree with flipping the argument error for three main reasons.

First: it violates the signature entirely
The signature to map is map(function, *iterables). Python’s map is more like Haskell’s zipWith. Making the function last would either ruin the signature or would slow down performance.

Second: currying
If you ever curry a function in Python using functools.partial, having the most common arguments first is crucial. (You’re more likely to apply the same function to multiple iterables than to apply several functions on the same exact iterable).

Thirdly: the change would make several functional programming packages have incompatible APIs.
Currently libraries like PyToolz/Cytoolz and funcy have APIs that require function-first argument order. Changing the argument order would be disruptive to most Python FP packages/frameworks.

So while I agree with you that “iterable.map(fn)” is more readable, I think changing the argument order would be too much of a breaking change, and there is no practical way to add “iterable.map(fn)” to every iterable type.

- Ed

> On Sep 13, 2017, at 11:09, Jason H <jhihn at gmx.com> wrote:
> 
> The format of map seems off. Coming from JS, all the functions come second. I think this approach is superior.
> 
> Currently:
> map(lambda x: chr(ord('a')+x), range(26)) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
> 
> But I think this reads better:
> map(range(26), lambda x: chr(ord('a')+x))
> 
> Currently that results in:
> TypeError: argument 2 to map() must support iteration
> 
> Also, how are we to tell what supports map()?
> Any iterable should be able to map via:
> range(26).map(lambda x: chr(ord('a')+x)))
> 
> While the line length is the same, I think the latter is much more readable, and the member method avoids parameter order confusion
> 
> For the global map(),
> having the iterable first also increases reliability because the lambda function is highly variable in length, where as parameter names are generally shorter than even the longest lambda expression.
> 
> More readable: IMHO:
> map(in, lambda x: chr(ord('a')+x))
> out = map(out, lambda x: chr(ord('a')+x))
> out = map(out, lambda x: chr(ord('a')+x))
> 
> Less readable (I have to parse the lambda):
> map(lambda x: chr(ord('a')+x), in)
> out = map(lambda x: chr(ord('a')+x), out)
> out = map(lambda x: chr(ord('a')+x), out)
> 
> But I contend:
> range(26).map(lambda x: chr(ord('a')+x)))
> is superior to all.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



More information about the Python-ideas mailing list