[Python-ideas] Vectorization [was Re: Add list.join() please]
Steven D'Aprano
steve at pearwood.info
Thu Feb 7 00:02:52 EST 2019
Before I respond to a specific point below, I'd like to make a general
observation.
I changed the subject line of this sub-thread to discuss a feature of
Julia, which allows one to write vectorized code in standard infix
arithmetic notation, that applies to any array type, using any existing
function or operator, WITHOUT having to wrap your data in a special
delegate class like this "Vector".
So as far as I'm concerned, this entire discussion about this wrapper
class misses the point.
(Aside: why is this class called "Vector" when it doesn't implement a
vector?)
Anyway, on to my response to a specific point:
On Mon, Feb 04, 2019 at 11:12:08AM -0500, David Mertz wrote:
> On Mon, Feb 4, 2019 at 7:14 AM Kirill Balunov <kirillbalunov at gmail.com>
> wrote:
>
> > len(v) # -> 12
> >
> > v[len] # -> <Vector of [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]>
> >
> >
> > In this case you can apply any function, even custom_linked_list from
> > my_inhouse_module.py.
> >
>
> I think I really like this idea. Maybe as an extra spelling but still
> allow .apply() to do the same thing. It feels reasonably intuitive to me.
> Not *identical to* indexing in NumPy and Pandas, but sort of in the same
> spirit as predicative or selection based indices.
>
> What do other people on this thread think? Would you learn that easily?
obj[len] already has an established meaning as obj.__getitem__(len).
There's going to be clash here between key lookup and applying a
function:
obj[len] # look up key=len
obj[len] # apply function len
Mathematica does use square brackets for calling functions, but it uses
ordinary arithmetic order len[obj] rather than postfix order obj[len].
At the risk of causing confusion^1, we could have a "vector call"
syntax:
# apply len to each element of obj, instead of obj itself
len[obj]
which has the advantage that it only requires that we give functions a
__getitem__ method, rather than adding new syntax. But it has the
disadvantage that it doesn't generalise to operators, without which I
don't think this is worth bothering with.
^1 Cue a thousand Stackoverflow posts asking whether they should use
round brackets or square when calling a function, and why they get weird
error messages sometimes and not other times.
--
Steven
More information about the Python-ideas
mailing list