Re: multiple variables in pure function used in map.
- To: mathgroup at smc.vnet.net
- Subject: [mg107647] Re: multiple variables in pure function used in map.
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 21 Feb 2010 04:25:18 -0500 (EST)
- References: <hlohjt$d95$1@smc.vnet.net>
pipehappy schrieb:
> Hi, All
>
> Is there a way to use multiple variables in pure function with map.
>
> What I want to do is like this:
>
> ab = {{1, 2}, {2, 3}, {3, 4}};
> (#[[1]] + #[[2]]) & /@ ab
> {3, 5, 7}
>
> Instead of refer to elements in list I want to use multiple variables
> in pure function.
>
> something like this:
> ab = {{1, 2}, {2, 3}, {3, 4}};
> (#1 + #2) & /@ ab
> To do the same thing as the above example.
>
> Best
>
> -- pipehappy
>
Yes, but you have to "Apply" the function instead of "Map"ping it:
In[1]:= ab=Partition[Range[4],2,1]
Out[1]= {{1,2},{2,3},{3,4}}
In[2]:= (#1+#2)& @@@ ab
Out[2]= {3,5,7}
In[3]:= (* or simply *)
Plus @@@ ab
Out[3]= {3,5,7}
Peter