       Re: Destructuring arguments to pure functions?

• To: mathgroup at smc.vnet.net
• Subject: [mg95756] Re: Destructuring arguments to pure functions?
• From: dreeves <dreeves at gmail.com>
• Date: Mon, 26 Jan 2009 05:03:07 -0500 (EST)

```I'm curious what you all think of this idea:

Unprotect[Integer];
1[x_] := x[]
2[x_] := x[]
3[x_] := x[]
4[x_] := x[]
5[x_] := x[]
6[x_] := x[]
7[x_] := x[]
8[x_] := x[]
9[x_] := x[]
Protect[Integer];

and then

1@# + 2@# & /@ testList

It seems to be very slow in the current version of Mathematica but it
seems like a concise/elegant way to avoid the clunky yet oft-typed #
[[n]] (ie, replacing it with n@# or n[#]).

Perhaps more generally, other sensible behavior could be defined for
expressions with non-symbols as the Head for which the meaning is
currently undefined.

On Dec 29 2008, 6:41 am, Stoney Ballard <ston... at gmail.com> wrote:
> I hate using #[] etc. to access elements of list arguments to pure
> functions, so I've been looking for a way around that.
>
> One way is to simply stop using pure functions in those cases, and use
> locally bound function definitions, like
>
> testList = Flatten[Table[{x, y}, {x, 1, 100}, {y, 1, 100}], 1];
>
> (Example 1)
>
> Module[{fun},
>   fun[{x_, y_}] := x + y;
>   Map[fun, testList]]
>
> This turns out to be significantly faster than any other method I've
> tried, but it seems clunky and requires Module.
>
> Another way is to use RuleDelayed:
>
> (Example 2)
>
> Map[# /. {x_, y_} :> x + y &, testList]
>
> but this is about 5 times slower than example 1. Binding the rule with
> Module:
>
> (Example 3)
>
> Module[{fun = {x_, y_} :> x + y},
>    Map[# /. fun &, testList]]
>
> Is only marginally slower than example 1, but still requires Module or
> With. Strangely (to me), I found that binding the rule as a pure
> function:
>
> (Example 4)
>
> Module[{fun = # /. {x_, y_} :> x + y &},
>  Map[fun, testList]]
>
> is about the same speed as example 2, or 5 times slower than example
> 1. I don't understand why there should be so much of a difference
> there.
>
> Finally, doing this with Part:
>
> (Example 5)
>
> Map[#[] + #[] &, testList]
>
> is 2-3 times faster than using destructuring in example 1.
>
> Clearly, I need to give up some performance to get the clarity of
> destructuring, but is there a better way than example 1?

```

• Prev by Date: Partial derviatives in mathematica
• Next by Date: Re: Mathematica and LyX - Graphics and equations
• Previous by thread: Re: Partial derviatives in mathematica
• Next by thread: Re: Destructuring arguments to pure functions?