[Python-ideas] Vectorization [was Re: Add list.join() please]
Steven D'Aprano
steve at pearwood.info
Sat Feb 2 18:30:40 EST 2019
On Sat, Feb 02, 2019 at 06:08:24PM -0500, David Mertz wrote:
> In terms of other examples:
>
> map(str.upper, seq) uppercases each item
> map(operator.attrgetter('name'), seq) gets the name attribute of each item
> map(lambda a: a*2, seq) doubles each item
Now compose those operations:
((seq .* 2)..name)..upper()
versus
# Gag me with a spoon!
map(str.upper, map(operator.attrgetter('name'), map(lambda a: a*2, seq)))
The comprehension version isn't awful:
[(a*2).name.upper() for a in seq]
but not all vectorized operations can be written as a chain of calls on
a single sequence.
There are still some open issues that I don't have good answers for.
Consider ``x .+ y``. In Julia, I think that the compiler has enough type
information to distinguish between the array plus scalar and array plus
array cases, but I don't think Python will have that. So possibly there
will still be some runtime information needed to make this work.
The dot arguably fails the "syntax should not look like grit on Tim's
monitor" test (although attribute access already fails that test). I
think the double-dot syntax looks like a typo, which is unfortunate.
--
Steve
More information about the Python-ideas
mailing list