# Mapping a pure function with multiple slots [duplicate]

Here is a pure function with multiple slots:

(#1^2 + #2^2)&[3, 4]


25

But how can I map that pure function over lists of values? For example, say I want to map (#1^2 + #2^2)& over x = {1, 2, 4} and y = {3, 4, 6}. I have tried the following expressions, but none of them work:

Map[(#1^2 + #2^2) &, {1, 2, 4}, {3, 4, 6}]
Map[(#1^2 + #2^2) &, {{1, 2, 4}, {3, 4, 6}}]
Map[(#1^2 + #2^2) &, {{1, 3}, {2, 4}, {4, 6}}]


The result I expect is obviously {10, 20, 52}.

Could some one help me with this?

• (#1^2 + #2^2) & @@@ Transpose[{{1, 2, 4}, {3, 4, 6}}] Commented Jun 26, 2013 at 22:46
• Also, don't forget that x^2+y^2 works, too. Commented Jun 27, 2013 at 16:00
• Related: (5217), (10211), (24939) Commented Jun 28, 2013 at 0:39

MapThread[#1^2 + #2^2 &, {x, y}]

• Thank you very much, PatoCriollo. I will remember this function definitely! Commented Jun 26, 2013 at 22:47
• En mi pueblo al Pato Criollo le dicen catrasca Commented Jun 26, 2013 at 23:10
• Thanks you, m_goldberg, it reads much more comfortable. Commented Jun 26, 2013 at 23:29
• En mi pueblo (Quilmes) tambien @belisarius Commented Jun 29, 2013 at 15:23
• Welcome to the club, then Commented Jun 30, 2013 at 13:43

I like the following very much

{x, y} = {{1, 2, 4}, {3, 4, 6}};
(#1^2 + #2^2) & @@@ Transpose[{x,y}]


Another thing which is highly unused is to attach Attributes to pure Functions

Function[{a, b}, a^2 + b^2, {Listable}][x, y]


You can stick with the Slot notation too, but you have to tell then that the variables in Function is a Null list which might look too awkward for some users (but I give this as dedication to Leonid, who uses this quite a bit)

Function[Null, #1^2 + #2^2, {Listable}][x, y]


and to really mess it up, I'd like to remind everyone, that you don't need to put the Null explicitly. Therefore, this is completely valid syntax

Function[, #1^2 + #2^2, {Listable}][x, y]

• Isn't attaching {Listable} the same as using MapThread from PatoCriollo's answer? Commented Jun 26, 2013 at 23:01
• @BlacKow Nope, try to do this with MapThread: Function[{x, y}, x + y, {Listable}][1, {1, 2, 3}]. On the other hand with MapThread you can specify the level of depth. Commented Jun 26, 2013 at 23:06
• I'm really just starting to learn Mathematica so I have a confusing mix of Map,Apply,Thread in my head. I was just saying that there are two different families of possible solutions: based on Threading and based on Applying. Is it correct? Commented Jun 26, 2013 at 23:15
• @BlacKow sort of. Consider Map to be your generic method for transforming everything in a list/expression, e.g. f /@ a[q,r,s] becomes a[f[q], f[r], f[s]] Apply changes the Head of an expression, e.g. Times@@(a+b) becomes Times[a, b]. So, it does not work on atomic expressions, i.e. if AtomQ@e == True Apply won't work on it. (Map doesn't have that restriction.) Otherwise, it is similar to MapThread. Thread, however, is in some ways a generic distribution function for lists, e.g. Thread[a -> {q, r}] becomes {a->q, a->r}. So, subtly different applications. Commented Jun 27, 2013 at 3:11
• @rcollyer thank you, this was my thought as well. Substituting the Head versus Mapping Commented Jun 27, 2013 at 3:15

Referring to the responses, maybe it is more elegant this way?

For single slot:

#^2 & @ {1,2,4}

[Out]={1, 4, 16}


For multiple slots:

(#1^2 + #2^2) & @@ {{1, 2, 4}, {3, 4, 6}}

[Out]= {10, 20, 52}


The following codes have same output:

MapThread[(#1^2 + #2^2) &, {{1, 2, 4}, {3, 4, 6}}]

Apply[(#1^2 + #2^2) &, {{1, 2, 4}, {3, 4, 6}}]


It seems that Apply and MapThread are exchangeable in this case!