MathGroup Archive 2009

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

Search the Archive

Re: Re: Destructuring arguments to pure functions?

  • To: mathgroup at
  • Subject: [mg95810] Re: [mg95787] Re: Destructuring arguments to pure functions?
  • From: "David Park" <djmpark at>
  • 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}


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

But this does not work for:

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

However, with 


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

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  

From: Bill Rowe [mailto:readnews at] 

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

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

>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]]

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:


  • 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...