Re: Nesting functional commands
- To: mathgroup at smc.vnet.net
- Subject: [mg102937] Re: Nesting functional commands
- From: pfalloon <pfalloon at gmail.com>
- Date: Wed, 2 Sep 2009 04:06:25 -0400 (EDT)
- References: <h7ijpu$i7f$1@smc.vnet.net>
On Sep 1, 5:51 pm, Erin Noel <eno... 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 As it happens, the example you give works exactly as you would want it to: In[215]:= A = Range[4]; Select[3*#& /@ A, #<7&] Out[216]= {3,6} For most such cases, Mathematica is able to understand which function each # belongs to. In the case above, the two arguments to Select are evaluated one after the other, so there is no chance of confusing the sense of each #. More generally, my experience is that Mathematica can parse complicated nested anonymous functions better than humans (or at least, better than me), so good programming style dictates that it is rarely necessary to nest things in a way that risks confusion (and IMO, where confusion is even remotely possible, it's much better to define intermediate variables so that your code makes sense). Cheers, Peter.