From: Adriano Pascoletti on
(#1 + #2) & @@@ ab


Plus @@@ ab


Total/@ab


Adriano Pascoletti

2010/2/20 pipehappy <pipehappy(a)gmail.com>

> 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
>
>


From: Peter Pein on
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


From: Bill Rowe on
On 2/20/10 at 6:38 AM, pipehappy(a)gmail.com (pipehappy) wrote:

>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.

Use Apply. For example,

In[5]:= Plus @@ # & /@ ab

Out[5]= {3,5,7}

Or perhaps better, dispense with Map and do

In[6]:= Plus @@@ ab

Out[6]= {3,5,7}

Or

In[7]:= Apply[Plus, ab, {1}]

Out[7]= {3,5,7}



From: Bob Hanlon on

ab = {{1, 2}, {2, 3}, {3, 4}};

(#1 + #2) & @@@ ab

{3,5,7}

which is shorthand for

Apply[(#1 + #2) &, ab, 1]

{3,5,7}

However, for this specific case

Plus @@@ ab

{3,5,7}

Total /@ ab

{3,5,7}


Bob Hanlon

---- pipehappy <pipehappy(a)gmail.com> wrote:

=============
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


From: Leonid Shifrin on
Hi,

what you need is to Apply your function (Plus here) to level 1 of your
expression:

In[1]:= ab = {{1, 2}, {2, 3}, {3, 4}};

The shorthand is @@@:

In[2]:= (#1 + #2) & @@@ ab

Out[2]= {3, 5, 7}

You can also use Plus, which has the advantage that you don't need to know
the size of your sublists - they may even have different sizes.

In[3]:= Plus @@@ ab

Out[3]= {3, 5, 7}

Without the shorthand, it looks like:

In[4]:= Apply[(#1 + #2) &, ab, 1]

Out[4]= {3, 5, 7}

In[5]:= Apply[Plus, ab, 1]

Out[5]= {3, 5, 7}

You can also use Map:

In[6]:= Map[Apply[(#1 + #2) &, #] &, ab]

Out[6]= {3, 5, 7}

In[7]:= Map[Apply[Plus, #] &, ab]

Out[7]= {3, 5, 7}

In your particular case, it will probably most efficient to use Total
with level specification:

In[8]:= Total[ab, {2}]

Out[8]= {3, 5, 7}

Hope this helps.

Regards,
Leonid




On Sat, Feb 20, 2010 at 3:38 AM, pipehappy <pipehappy(a)gmail.com> wrote:

> 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
>
>