[Python-ideas] Make map() better

David Mertz mertz at gnosis.cx
Wed Sep 13 11:20:30 EDT 2017


We're not going to break every version of Python since 0.9 because
Javascript does something a certain way.  Whatever might be better
abstractly, this is well established.

As to adding a `.map()` method to *every* iterable... just how would you
propose to do that given that it's really easy and common to write custom
iterables.  How do you propose to change every class ever written by users?
(including ones that already define `.map()` with some other meaning than
the one you suggest)?

On Wed, Sep 13, 2017 at 8:09 AM, 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/
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170913/77ea9ab7/attachment-0001.html>


More information about the Python-ideas mailing list