MathGroup Archive 2009

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

Search the Archive

Re: Nesting functional commands

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102901] Re: [mg102875] Nesting functional commands
  • From: "Elton Kurt TeKolste" <tekolste at fastmail.us>
  • Date: Wed, 2 Sep 2009 03:59:57 -0400 (EDT)
  • References: <200909010751.DAA18675@smc.vnet.net>

It seems to me that Select[3*# & /@ A, # < 7 &] works fine.  It is
perfectly clear to the reader and to Mathematica which # is scoped with
which &.

In case that this is an example extracted to illustrate a more complex
situation, note that there is always the option of making the function
explicit (& -> Function) and naming the dummy variables.

Select[Function[x,3x]/@A,Function[y,y<7]]

If it helps the clarity of your code, you can even define the functions
separately:

In[23]:= f1 = Function[x, 3 x]

Out[23]= Function[x, 3 x]

In[24]:= f2 = Function[x, x < 7]

Out[24]= Function[x, x < 7]

In[25]:= Select[f1 /@ A, f2]

Out[25]= {3, 6}

Kurt

On Tue, 01 Sep 2009 03:51 -0400, "Erin Noel" <enoel2 at gmail.com> wrote:
> Hi,
> 
> So how would you go about nesting a functional command inside of another
> functional command? For example, say A={1,2,3,4}, then 3*#&/@A =
> {3,6,9,12}.
> No problem. But then what if, within that vector {3,6,9,12}, I wanted to
> use
> the Select command to choose elements less than, say 7. The command
> Select[3*#&/@A,#<7&] doesn't work because, as far as I can tell,
> Mathematica
> doesn't know which "#" belongs to Select and which "#" belongs to the &/@
> mapping over A. I know that I could define 3*#&/@A as a new variable and
> then use that new variable in the Select command, but is there any way to
> nest multiple commands in this way?
> 
> Thanks a lot in advance,
> Erin
> 
> 
Regards,
Kurt Tekolste



  • Prev by Date: Re: Faster alternative to AppendTo?
  • Next by Date: Re: Faster alternative to AppendTo?
  • Previous by thread: Re: Nesting functional commands
  • Next by thread: Re: Nesting functional commands