From: pipehappy on
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: Norbert P. on
Hey,

use Apply with level specification:

Apply[(#1 + #2) &, ab, {1}]
{3, 5, 7}

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

In this particular case even better
Plus @@@ ab

Best,
Norbert


On Feb 20, 3:38 am, pipehappy <pipeha...(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: Chris Degnen on
The trick is applying sequence to the input sublists:

ab = {{1, 2}, {2, 3}, {3, 4}};
Map[Function[{x, y}, x + y][Apply[Sequence, #]] &, ab]
(* alternatively *)
(#1 + #2) &[Apply[Sequence, #]] & /@ ab



> 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: Raffy on
On Feb 20, 3:38 am, pipehappy <pipeha...(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

Apply (at level 1) is what you need.

Recall, FullForm of {{1,2}, {3,4}} is List[List[1,2], List[3,4]]

Apply[h, ex, level] replaces the head of each expression at level of
ex with h.

So, Apply[func, List[List[1,2], List[3,4]], {1}] will give you
List[func[1,2], func[3,4]].

The shorthand for Apply at level {1} is @@@.

#1+#2&@@@{{1,2},{3,4}}

However, this is just Plus@@@{{1,2},{3,4}}, or better: Total[{{1,2},
{3,4}},{2}].

From: David Park on
ab = {{1, 2}, {2, 3}, {3, 4}};

Apply[#1 + #2 &, #] & /@ ab
{3, 5, 7}


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: pipehappy [mailto: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