Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the extra {} when Mapping a function to a list

Simple question, given a list like this

Clear[a, b, c, d, e, f];
lst = {{a, b}, {c, d}, {e, f}};

and suppose I have a function defined like this:

foo[x_,y_]:=Module[{},...]

And I want to apply this function to the list, so If I type

Map[foo, lst]

This gives

{foo[{a, b}], foo[{c, d}], foo[{e, f}]}

I want it to come out as

{foo[a, b], foo[c, d], foo[e, f]}

so it works.

What is the best way to do this? Assume I can't modify the function foo[] definition (say it is build-in)

Only 2 ways I know now are

Map[foo[#[[1]], #[[2]]] &, lst]
{foo[a, b], foo[c, d], foo[e, f]}

(too much work), or

MapThread[foo, Transpose[lst]]
{foo[a, b], foo[c, d], foo[e, f]}

(less typing, but need to transpose first)

Question: Any other better ways to do the above? I looked at other Map and its friends, and I could not see a function to do it more directly than what I have.

like image 280
Nasser Avatar asked Jan 06 '12 22:01

Nasser


1 Answers

You need Apply at Level 1 or its short form, @@@

foo@@@lst    
{foo[a, b], foo[c, d], foo[e, f]}
like image 161
abcd Avatar answered Sep 20 '22 18:09

abcd