But it doesn't work. Why is that?
It becomes visible when you inspect the inner Map
only. I replace the slot for the outer function with 1, because we don't need it to see the error
b = {1, 2};
c = {1, 2, 3};
Map[f[# &, 1], b]
(* {f[#1 &, 1][1], f[#1 &, 1][2]} *)
This is not what you expect and when you look a bit closer, you instantly see that your function is not a Function
f[# &, 1] // FullForm
(* f[Function[Slot[1]],1] *)
Therefore, evaluating f[#&,1][blub]
does not result in f[blub,1]
as you need it. Now you may think, OK, the &
is at the wrong place, it needs to be left to the closing ]
of f
like this f[#,1]&
. Yes, but now you instantly run into trouble when you replace the 1 with your second #
again, because now, the slot belongs to the inner function, which is not what you want.
The direct solution is fairly simple. Replace one anonymous function with an explicit Function
where you can name the argument
b = {1, 2};
c = {1, 2, 3};
Map[Function[arg, Map[f[#, arg] &, b]], c]
(* {{f[1, 1], f[2, 1]}, {f[1, 2], f[2, 2]}, {f[1, 3], f[2, 3]}} *)
If you have understood your mistake, you may want to think using a more direct approach. The one suggested by Spawn in the comment is really short and readable
Outer[f, c, b]
The function Outer
is probably not known by new Mathematica users. One function which is one of the first you learn is Table
Table[f[bi, ci], {ci, c}, {bi, b}]
Outer
:Outer[f,c,b]
for example. Your solution wasn't absolutely wrong the only problem wasf[# &, #]
It should have been:Map[Function[$x$,Map[f[# , $x$]&, b]], c]
$\endgroup$f
likef[x_][y_]:=Sin[x y]
then this works greatf[#] /@ b & /@ c
. $\endgroup$