Re: pure functions
- To: mathgroup at smc.vnet.net
- Subject: [mg23287] Re: pure functions
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 30 Apr 2000 21:13:37 -0400 (EDT)
- References: <8eg4bn$g5i@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Helge, (1) Some ideas data = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; The first five below work even when the subsequences are of different lengths. The symbol ## (SlotSequence[1]) is useful since it will receive any number of inputs. Plus[##] &[Sequence @@ Transpose[data]] {3, 7, 11, 15} Apply[Plus, data, {1}] {3, 7, 11, 15} Plus @@@ data (*in version 4.0x*) {3, 7, 11, 15} Plus @@ Transpose[data] {3, 7, 11, 15} The following only works when the subsequences are of the same length. data.Table[1, {Dimensions[data][[2]]}] {3, 7, 11, 15} (2) Select[{1, 2, 3, 4, 5, 6}, # > 3 &] {4, 5, 6} Works by tresting the successive entries in [{1, 2, 3, 4, 5, 6}, with the function # > 3 &. An entry is selected if an only if this gives True. Thus # > 3 &[2] False # > 3 &[4] True -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Helge Andersson" <helgea at inoc.chalmers.se> wrote in message news:8eg4bn$g5i at smc.vnet.net... > Hello, > > > Of all the nice functions such as Map, Mapall, Thread, Apply .... I have > not been able to write a simple code to generate the following > procedure. > > I have a two dimensional list like > > li={{11,12,13,..},{21,22,23,...},{31,32,33,..},...} > > Since i like to use the pure function command I would like to map my > pure function with arguments #1,#2,#3,.... on all the sublists in li. > > Let me exemplify with a simple pure function that add to numbers. > (#1+#2)& > > if exli={{1,2},{3,4},{5,6},{7,8}} > > then I want to get the result > {3,7,11,15} > > One solution, but not allways suitable for me, is the following > > (#1+#2)&[Sequence @@ Transpose[exli]]. > > I want to get rid of the Transpose command and if possible also making > use of the /@ notation for Map. Since I have seen so many elegant > examples in the mailing lists I hope I can get something out of this. > > Finally, When using pure functions inside Mathematica defined functions > such as Select for instance, > Select[{1,2,3,4,5,6},#>3&] > why don't we need to specify the argument list after the &sign. I can > figure out that in this case the list sent to the Select command will be > used as argument list for the pure function but how does this work in > general. Which are the functions where this feature is possible? > > /Helge > > -- > ******************* > Helge Andersson > Oorganisk miljökemi > CTH > 412 96 Göteborg > > Tel. 031-772 8072 > Tel. Lab. 031-55 38 67 > Fax 031-772 28 53 > > >