MathGroup Archive 2010

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

Search the Archive

Re: Placing the "&"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114797] Re: Placing the "&"
  • From: Barrie Stokes <Barrie.Stokes at newcastle.edu.au>
  • Date: Fri, 17 Dec 2010 03:30:06 -0500 (EST)
  • References: <201012161048.FAA11804@smc.vnet.net>

Hi ziloo

Thanks for this question. It  gives me a soapbox chance, although treacherous waters are those discussing styles of any kind (de gustibus non disputandum est).

I once did some beta testing for a Mathematica application, and had a long dialogue with the author about this, and I think I convinced him then that the foolproof way to write pure functions is the form(...stuff with #...&). That way the bracket matching mechanism Ctrl-Shift-B will always cover the whole pure function unambiguously.

Thus, to take your examples,

Select[Range[1000], (PrimeQ[2^(#) + 1] &)]

and

Select[Range[500], (PrimeQ[FromDigits[Table[1, {#}]]] &)].

Now there is something better.

I always write such lines as

Select[Range[1000], (s \[Function] PrimeQ[2^s + 1])]

and

Select[Range[500], (s \[Function] PrimeQ[FromDigits[Table[1, {s}]]])].

Again the bounding parentheses, and, I think, easier to read.

In this way you can write such as:

(s \[Function] s^2) /@ {1, 2, 3, 4, 5}

(s \[Function] s^2) /@ {{1, 2}, {3, 4}, {5, 6}, {7, 8}}

(s \[Function] Sqrt[
\!\(\*SubscriptBox["s", 
RowBox[{"\[LeftDoubleBracket]", "2", 
      "\[RightDoubleBracket]"}]]\)]) /@ {{1, 2}, {3, 4}, {5, 6}, {7, 
   8}}

({s, t} \[Function] s^2 + t)[3, 4]

({s} \[Function] 
\!\(\*SubscriptBox["s", 
RowBox[{"\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]]\)^2 + 
\!\(\*SubscriptBox["s", 
RowBox[{"\[LeftDoubleBracket]", "2", 
      "\[RightDoubleBracket]"}]]\)) /@ {{1, 2}, {3, 4}, {5, 6}, {7, 8}}

Sort[ {{1, 9}, {1, 5}, {1, 3}, {1, 8}},  ({s, t} \[Function] 
\!\(\*SubscriptBox["s", 
RowBox[{"\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]]\) < 
\!\(\*SubscriptBox["t", 
RowBox[{"\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]]\))]

Sort[ {{"b", {1, 2, 3}}, {"a", {7, 8, 9}}},  ({s, t} \[Function] 
\!\(\*SubscriptBox["s", 
RowBox[{"\[LeftDoubleBracket]", 
RowBox[{"2", ",", "1"}], "\[RightDoubleBracket]"}]]\) > 
\!\(\*SubscriptBox["t", 
RowBox[{"\[LeftDoubleBracket]", 
RowBox[{"2", ",", "1"}], "\[RightDoubleBracket]"}]]\))]

Split[ {{1, 2}, {1, 4}, {5, 6}, {7, 8}}, ({s, t} \[Function] 
   s[[1]] === t[[1]]) ]

HTH.

Barrie




>>> On 16/12/2010 at 9:48 pm, in message <201012161048.FAA11804 at smc.vnet.net>,
ziloo <ziloo_1 at yahoo.com> wrote:
> Hello folks...a newbie question here,
> 
> Looking at the following three examples, would you please
> explain the criteria for placing the "&" when using an
> anonymous function:
> 
> 2^(#) +1 &
> 
> Select[Range[1000],PrimeQ[2^(#)+1]&]
> 
> Select[Range[500], PrimeQ[FromDigits[Table[1, {#}]]] &]
> 
> Why is the "&" getting further away from the function's body?
> 
> 
> Thank you
> 
> ziloo



  • Prev by Date: Re: Help with Loop to Rule Based Algorithm
  • Next by Date: Re: VectorPlot on a Circle
  • Previous by thread: Placing the "&"
  • Next by thread: Re: Placing the "&"