[Python-ideas] Vectorization [was Re: Add list.join() please]

Steven D'Aprano steve at pearwood.info
Thu Feb 7 00:13:31 EST 2019


On Sun, Feb 03, 2019 at 09:46:44PM -0800, Christopher Barker wrote:

> I've lost track if who is advocating what, but:

Ironically, I started this sub-thread in response to your complaint that 
you didn't like having to explicitly write loops/maps. So I pointed out 
that in Julia, people can use (almost) ordinary infix syntax using 
operators and function calls and have it apply automatically to each 
item in arrays.

It wasn't a concrete proposal, just food for thought. Unfortunately the 
thinking seems to have missed the point of the Julia syntax and run off 
with the idea of a wrapper class.

[...]
> I do not get the point of this at all -- we already have map"
> 
> map(v, lambda s s.replace()"a,", "b")

The order of arguments is the other way around. And you did say you 
didn't like map. Wouldn't you rather write:

    items.replace("a", "b")

rather than 

    map(lambda s: s.replace("a", "b"), items)

or

    [s.replace("a", "b") for s in items]


I know I would. Provided of course we could distinguish between 
operations which apply to a single string, and those which apply to a 
generic collection of strings.

Beside, while a single map or comprehension is bearable, more complex 
operations are horrible to read when written that way, but trivially 
easy to read when written in standard infix arithmetic notation. See my 
earlier posts for examples.


> >     v.replace("a", "b")
> >
> 
> This is adding something - maybe just compactness, but I also think
> readability.

Indeed. In Julia that also offers opportunities for the compiler to 
optimize the code, bringing it to within 10% or so of a C loop. 
Maybe PyPy could get there as well, but CPython probably can't.

> I've also lost track of whether anyone is proposing a "vector of strings'
> as opposed to a vector of arbitrary objects.

Not me.



-- 
Steven


More information about the Python-ideas mailing list