Re: Nesting functional commands
- To: mathgroup at smc.vnet.net
- Subject: [mg102909] Re: [mg102875] Nesting functional commands
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Wed, 2 Sep 2009 04:01:23 -0400 (EDT)
- References: <200909010751.DAA18675@smc.vnet.net>
Hi Erin, first of all, your construction Select[3*#&/@A,#<7&] will work all right. The ambiguity happens when you need to use slot variables belonging to pure functions of different nesting level, at the same time (in the same expression). A simple way to tell is to look at the FullForm: Select[ Map[ Function[Times[3, Slot[1]]], a], Function[Less[Slot[1], 7]]] This reveals that in your example, there is no nesting at all - pure functions are completely independent. However, even when the nesting happens, it is fine as long as variables referenced in more internal Function-s are not those which belong (logically) to external ones. Here is an example of a pure function containing several nested anonimous functions, but being ok: Split[Sort[#, First@#1 > First@#2 &], First@#1 == First@#2 &] & It takes a list, sorts it according to its first element in decreasing order, and then groups together sublists with the same first element: In[1] = test = RandomInteger[{1, 10}, {10, 2}] Out[1] = {{4, 5}, {7, 9}, {10, 7}, {8, 9}, {1, 6}, {2, 3}, {7, 5}, {8, 3}, {1, 1}, {9, 9}} In[2] = Split[Sort[#, First@#1 > First@#2 &], First@#1 == First@#2 &] &[test] Out[2] = {{{10, 7}}, {{9, 9}}, {{8, 3}, {8, 9}}, {{7, 5}, {7, 9}}, {{4, 5}}, {{2, 3}}, {{1, 1}, {1, 6}}} Using slots everywhere is possible in cases like above, where enitre inner pure functions are contained in outer ones as one of their arguments, but not possible in general. Here is an example of a function that takes a list, and then maps a function {first element,#}& on the rest of the list: Function[x, {First@x, #} & /@ Rest[x]] Here, it is not possible to use anonimous functions everywhere, because there is a place where both external and internal slot variable will be referenced at once - in the two-element sublist being mapped. Here is how it works: In[3] = Function[x, {First@x, #} & /@ Rest[x]][Range[5]] Out[3] = {{1, 2}, {1, 3}, {1, 4}, {1, 5}} An attempt to use slots in both functions here will lead to a run-time error since all slots in {First@#,#}& are interpreted as belonging to its (inner) scope: In[4]:= ({First@#,#}&/@Rest[#])&[Range[5]] During evaluation of In[4]:= First::normal: Nonatomic expression expected at position 1 in First[2]. >> During evaluation of In[4]:= First::normal: Nonatomic expression expected at position 1 in First[3]. >> During evaluation of In[4]:= First::normal: Nonatomic expression expected at position 1 in First[4]. >> During evaluation of In[4]:= General::stop: Further output of First::normal will be suppressed during this calculation. >> Out[4]= {{First[2],2},{First[3],3},{First[4],4},{First[5],5}} You may want to check out a chapter on pure functions in my book: http://www.mathprogramming-intro.org/book/node50.html where I also discuss this and related topics. Hope this helps. Regards, Leonid On Tue, Sep 1, 2009 at 11:51 AM, 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 > > >
- References:
- Nesting functional commands
- From: Erin Noel <enoel2@gmail.com>
- Nesting functional commands