I tried to understand the mechanism of pure function tricks. Imaging I have a nested list generated by:
list=Table[{p, q}, {p, 1, 9, 2}, {q, -p, p, 2}]
Result:
> {{{1, -1}, {1, 1}}, {{3, -3}, {3, -1}, {3, 1}, {3, 3}}, {{5,
> -5}, {5, -3}, {5, -1}, {5, 1}, {5, 3}, {5, 5}}, {{7, -7}, {7, -5}, {7, -3}, {7, -1}, {7, 1}, {7, 3}, {7, 5}, {7, 7}}, {{9, -9}, {9,
> -7}, {9, -5}, {9, -3}, {9, -1}, {9, 1}, {9, 3}, {9, 5}, {9, 7}, {9, 9}}}
And a function:
Fun[x_, y_] := Total[Sqrt[x^2 + y^2]]
I want the Fun to apply to each pair of the list
to calculate the sum of all the result.
Should I use something like Fun[#1,#2]&/@list
? But it never works out, idk why.Thanks for your help!
Apply
andMap
, both are great tools, both can be made to work in the same situations, but usually one is more suited to the task at hand. Compare the results off @@ {1, 2}
,f @@@ {{1, 2}, {3, 4}, {5, 6}}
, andf /@ {{1, 2}, {3, 4}, {5, 6}}
$\endgroup$