0
$\begingroup$

Can the expression be simplified as "Total[funca[a,#]] & /@ {x,y}"? What does the double of "& /@" mean? Where can I find some reference?

Updated

According to Mr.Wizard's answer, I now know the result. But I still can not understand the combination use of #&/@.

For example, the result from Out[1] to Out[6] looks so weird to me. Who can help to explain that?

x = {q, r, s};
y = {t, u, v};
In[1]:= Total[funca[a, #]] & /@ {x, y}

Out[1]:= {{a + q, a + r, a + s}, {a + t, a + u, a + v}}

In[2]:= Total[funca[a, #] & /@ #] & /@ {x, y}

Out[2]:= {funca[a, q] + funca[a, r] + funca[a, s], funca[a, t] + funca[a, u] + funca[a, v]}

In[3]:= Total[funca[a, #] & /@ # & /@ #] & /@ {x, y}

Out[3]:= {q + r + s, t + u + v}

In[4]:= Total[{a, #}] & /@ {x, y}

Out[4]:= {{a + q, a + r, a + s}, {a + t, a + u, a + v}}

In[5]:= Total[{a, #} & /@ #] & /@ {x, y}

Out[5]:= {{3 a, q + r + s}, {3 a, t + u + v}}

In[6]:= Total[{a, #} & /@ # & /@ #] & /@ {x, y}

Out[6]:= {q + r + s, t + u + v}

$\endgroup$

1 Answer 1

6
$\begingroup$
Total[funca[a,#] & /@ #] & /@ {x,y}

There are two Function expressions here which I will refer to as inner and outer. The inner function:

funca[a,#] &

Is Mapped to the sole argument of the outer function. It will transform a list or other expression like this:

funca[a,#] & /@ foo[1, 2, 3]
foo[funca[a,1], funca[a,2], funca[a,3]]

The outer function does this to its single argument and then Totals it, which for the above would be:

funca[a,1] + funca[a,2] + funca[a,3]
  • To clarify: in the snippet funca[a,#] & /@ # the left # (in funca[a,#]) will be filled by the argument of the inner function while the right # will be filled by the argument of the outer function.

This is done for each for x and y because it (the outer function) is also mapped across {x, y}.

A complete example:

x = {q, r, s};
y = {t, u, v};

Total[funca[a, #] & /@ #] & /@ {x, y}
{funca[a, q] + funca[a, r] + funca[a, s],
 funca[a, t] + funca[a, u] + funca[a, v]}

The operation could also be written:

Total /@ Map[funca[a, #] &, {x, y}, {2}]

Map[funca[a, #] &, {x, y}, {2}] ~Total~ {2}
$\endgroup$
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.