Proposal for new "anonymous" function syntax

Hello

Please take a look at this proposal for a new “anonymous” function syntax capable of matching structured arguments.

The sophisticated Mathematica user for sure is aware of and uses anonymous functions with named parameters.

{x, y}\[Function] x*y^2
%[a, b]

Out[1]= Function[{x, y}, x y^2]    
Out[2]= a b^2

So far so good.
Now lets try an other example, suppose you want e.g. prepend an index coordinate to some list of data.

MapIndexed[{y, x} \[Function] {x[[1]], y}]
%[{a, b, c, d}]

Out[3]= MapIndexed[Function[{y, x}, {x[[1]], y}]]    
Out[4]= {{1, a}, {2, b}, {3, c}, {4, d}}

Ok, also nice … but wait what about with the ugly x[[1]] ?

Wouldn’t it be much nicer to match the indexing of x[[1]]?

What about the following syntax:

MapIndexed[{y, {x}} ==> {x, y}] [{a, b, c, d}]

Well we can implement it, the following code defines our new anonymous function operator syntax:

DoubleLongRightArrow[l_, r__] :=With[{s = (Map[Pattern[#, _] &, l, {-1}] :> r)}, {##} /. s &]

And here we have it in action:

MapIndexed[{y, {x}}\[DoubleLongRightArrow]{x, y}]@{a, b, c, d}

Out[5]= {{1, a}, {2, b}, {3, c}, {4, d}}

Any comments welcome.

Robert

1 Like

I’ve been forwarding these types of ideas actually directly to Stephen some years ago, where we discussed a couple of hours on these ideas. My example was similar in idea, imagine that you could do e.g.:

Table[… , {k->v, Association[…]}]

it would bind the key and value and iterate over the association, but this could be extended to any ‘pattern’ there as ‘variable’. Similar for Function, Do, Sum, Product etc…