Re: Pure Function within a pure function
- To: mathgroup at smc.vnet.net
- Subject: [mg59299] Re: Pure Function within a pure function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 5 Aug 2005 01:21:27 -0400 (EDT)
- Organization: The Open University, Milton Keynes, U.K.
- References: <dcsbi3$pub$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
ggroup at sarj.ca wrote: > Hi, > > I'm wondering if it is possible to define a pure function within a > function, and if so, what syntax should I be using? > > I have some data that I want to filter, say: > > rawdata[[1]]= > time1 pt1-1 pt1-2 pt1-3 > time2 pt2-1 pt2-2 pt2-3 > time3 aborted! > > rawdata[[2]]= > time1 pta-1 pta-2 pta-3 > time2 aborted! > > I have a list of such data sets, and I want to apply a filter to remove > the "aborted" lines to each set. With a table command, this is fairly > easy: > > data = Table[ > Select[rawdata[[i]],Length[#]==4&], > {i,Length[rawdata]} > ] > > For readability (and lets face it, for pure asthetic value), I was > hoping to convert this to a function which I could map onto my rawdata > array. I tried something like: > > data = Select[#, Length[#]==4&]& /@ rawdata; > > But this doesn't work. I imagine the confusion is with the comparison > function Length[#]==4&, but I'm having no luck figuring out how to make > it less ambiguous. > > Any pointers would be much appreciated. > > Thanks! > > PS: I'm using version 5.2 on Windows. > Hi, Can you describe exactly what problem occurs? I mean what do you mean by "not working" since the following code seems to work pretty well? In[1]:= rawdata = {{{time1, pt11, pt12, pt13}, {time2, pt21, pt22, pt23}, {time3, "aborted!"}}, {{time1, pta1, pta2, pta3}, {time2, "aborted!"}}} Out[1]= {{{time1, pt11, pt12, pt13}, {time2, pt21, pt22, pt23}, {time3, "aborted!"}}, {{time1, pta1, pta2, pta3}, {time2, "aborted!"}}} In[2]:= data1 = Table[Select[rawdata[[i]], Length[#1] == 4 & ], {i, Length[rawdata]}] Out[2]= {{{time1, pt11, pt12, pt13}, {time2, pt21, pt22, pt23}}, {{time1, pta1, pta2, pta3}}} In[3]:= data2 = (Select[#1, Length[#1] == 4 & ] & ) /@ rawdata Out[3]= {{{time1, pt11, pt12, pt13}, {time2, pt21, pt22, pt23}}, {{time1, pta1, pta2, pta3}}} In[4]:= data1 == data2 Out[4]= True In[5]:= $Version Out[5]= "5.2 for Microsoft Windows (June 20, 2005)" Best regards, /J.M.