MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Destructuring arguments to pure functions?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95810] Re: [mg95787] Re: Destructuring arguments to pure functions?
  • From: "David Park" <djmpark at comcast.net>
  • Date: Wed, 28 Jan 2009 06:27:30 -0500 (EST)
  • References: <4010584.1233058899703.JavaMail.root@m02>

Mathematica has many functions that are somewhat inconvenient because they
use parameters to operate on expressions and mix the expression into the
argument list. It is often convenient to redefine these as operators.

function[expr_, parameters__] would become function[parameters__][expr_].


In the case of Part, expr[[parameters]] is a kind of postfix form. The
inconvenience here is that all the brackets can be difficult to read in a
long piece of code. So one could define:

PartP[spec__][expr_] := Part[expr, spec]

and then use it as:

{a, b, c, d, e, f} // PartP[2 ;; 4]
{b, c, d}

or

PartP[2 ;; 4]@{a, b, c, d, e, f}
{b, c, d}

Then for:

testlist = {1, 2, 3, 4};

we could write:

testlist // (PartP[1] + PartP[4]) // Through
5


But this does not work for:

testlist // (PartP[1] - 2 PartP[4]) // Through
1 + (-2 PartP[4])[{1, 2, 3, 4}]

However, with 

Needs["Presentations`Master`"]

testlist // (PartP[1] - 2 PartP[4]) // PushOnto[_PartP]
-7


Maybe this is not much of an improvement, but it does avoid the pure
function and it stands out better when used in larger pieces of code.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  






From: Bill Rowe [mailto:readnews at sbcglobal.net] 

On 1/26/09 at 5:03 AM, dreeves at gmail.com (dreeves) wrote:

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

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

Redefining built-in objects often leads to unpredictable
behavior elsewhere in Mathematica. And doing this to more
fundamental objects tends to dramatically increase the
probability of something like this happening.

>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[#]).

There are better ways to avoid the usage of # than adding
definitions to Integer. In this particular example, you are
adding sequential pairs. This can be done without # as:

Plus@@@Partition[testList,2,1]





  • Prev by Date: Re: NIntegrate and Plot
  • Next by Date: Re: Problem with spline function object in Mathematica 6
  • Previous by thread: Re: Destructuring arguments to pure functions?
  • Next by thread: Permutations...