Re: Pure Function within a pure function
- To: mathgroup at smc.vnet.net
- Subject: [mg59320] Re: Pure Function within a pure function
- From: "dkr" <dkrjeg at adelphia.net>
- Date: Fri, 5 Aug 2005 01:22:48 -0400 (EDT)
- References: <dcsbi3$pub$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
When nesting pure functions, it is frequently prudent to avoid using
the shorthand notation for pure functions, since confusion can arise as
to which pure function # is referring to. Using the longhand notation
for pure functions, what you want to do is:
Function[x,Select[x,Function[y,Length[y]==4]]]/@rawdata
There are two pure functions involved. The outer one, with formal
parameter x, will, in turn, let each of the level 1 lists in rawdata
play the role of x. For a given list for x, this list becomes the first
coordinate of Select. Select then applies the pure function appearing
in its second coordinate to each sublist of x (y, the formal parameter
of this pure function, now playing in turn the role of each sublist of
x) . By using two different formal parameters it is easy to keep track
of what's going on.
In[10]:=
rawdata={{{time1, pt1-1,pt1-2,pt1-3},{time2,pt2-1,pt2-2,pt2-3},{time3,
aborted}},{{time1, pta-1,pta-2,pta-3},{time2,aborted}}};
In[14]:=
Table[ Select[rawdata[[i]],Length[#]==4&], {i,Length[rawdata]}]
Out[14]=
{{{time1,-1+pt1,-2+pt1,-3+pt1},{time2,-1+pt2,-2+pt2,-3+pt2}},{{time1,-1+
pta,-2+pta,-3+pta}}}
In[15]:=
Function[x,Select[x,Function[y,Length[y]==4]]]/@rawdata
Out[15]=
{{{time1,-1+pt1,-2+pt1,-3+pt1},{time2,-1+pt2,-2+pt2,-3+pt2}},{{time1,-1+
pta,-2+pta,-3+pta}}}
In[17]:=%14===%15
Out[17]=True
A closer look at your formulation yields the following:
In[19]:=
HoldForm[Select[#, Length[#]==4&]& /@ rawdata]//FullForm
Out[19]//FullForm=
HoldForm[Map[Function[Select[Slot[1],Function[Equal[Length[Slot[1]],4]]]],
rawdata]]
The above output suggests your formulation should work--there is no
confusion which Slot[1] refers to which pure function.
In[20]:=
Select[#, Length[#]==4&]& /@ rawdata
Out[20]=
{{{time1,-1+pt1,-2+pt1,-3+pt1},{time2,-1+pt2,-2+pt2,-3+pt2}},{{time1,-1+
pta,-2+pta,-3+pta}}}
In[21]:=
%20===%15
Out[21]=
True