MathGroup Archive 2010

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

Search the Archive

Re: Re: What does & mean?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107177] Re: [mg107155] Re: What does & mean?
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 5 Feb 2010 03:19:14 -0500 (EST)
  • References: <201002041126.GAA29847@smc.vnet.net>

Hi Bill,

give here is not a pure function. In fact
>
> Map[g[#]&, data]
>
> evaluated identically to
>
> Map[g, data]
>
> in every respect. The second definition using Slot (#) and &
> simply uses more characters to defined exactly the same operation.
>

Generally, there is one difference which can be pretty important at times
(not in the case you were discussing though) : that is, extra #-& introduce
an extra parameter-passing stage, and the passed parameters are not held
(allowed to evaluate). Example:

In[1]:= ClearAll[fn];
SetAttributes[fn, HoldAll];
fn[arg_] :=
  Switch[Head[Unevaluated[arg]],
   Print, "You want to print",
   Times, "You want to multiply",
   _, "I have no idea what you want to do"];

In[4]:=  fn[Print["*"]]

Out[4]= "You want to print"

In[6]:= fn[#] &@Print["*"]

During evaluation of In[6]:= *

Out[6]= "I have no idea what you want to do"

I think it's a pity that this subtle point is not mentioned explicitly in
the documentation of Function, where in section <properties and relations>
you find instead the following not always correct (and therefore
misleading)  statement:

---------------------------------------------------------

f[#]& is the same as simply f in the univariate case:

In[1]:= f[#] &[a]

Out[1]= f[a]

---------------------------------------------------------

Admittedly this is not something the beginner user is likely to run into
very often, but when /if
she does, there will be little help in consulting the current documentation.

Regards,
Leonid






>
> >We also can evaluate the desired root function dynamically using
> >ampersand, thus dispensing with the need to create a separate static
> >called function in the first place -
>
> >Map[#*Sin[#] &, data]
>
> This is a pure function. The difference here is the expression
> #*Sin[#}& has no name. That is what makes it a pure function.
>
> >The above syntax can be condensed further into a highly symbolic
> >form by using the symbolic invocation of Map function /@ -
>
> >#*Sin[#] & /@ data
>
> The /@ is simply a convenient shorthand for Map, nothing more.
> And in this particular case, the two operations used to define
> the pure function have the attribute Listable so the Map
> function is not needed and the same result can be obtained using
>
> #*Sin[#]&[data]
>
> or
>
> #*Sin[#]&@data
>
> or
>
> #*Sin@#&@data
>
> All I've done here is use different shorthand notation for doing
> exactly the same thing. While the last entry uses the fewest
> keystrokes to create, it is also probably the most arcane from
> the perspective of someone unfamiliar with Mathematica notation.
> And in fact, this version is probably more difficult to quickly
> read and understand by those quite familiar with Mathematica notation.
>
> In code I write for myself, the key reason I use various
> shorthand notations is to make the resulting code more easily readable.
>
>
>



  • Prev by Date: Weird vanishing syntax coloring
  • Next by Date: arrows disappear in exported 3D graphic
  • Previous by thread: Re: What does & mean?
  • Next by thread: Re: What does & mean?