Unexpected behavior with Map

As noted in comments, the standard ways to evaluate such result is to use Composition or pure function.

Using Composition:

Map[Minus@*f, Range[10]]

{-f[1], -f[2], -f[3], -f[4], -f[5], -f[6], -f[7], -f[8], -f[9], -f[10]}

Using pure function:

Map[(-f[#1])&, Range[10]]

{-f[1], -f[2], -f[3], -f[4], -f[5], -f[6], -f[7], -f[8], -f[9], -f[10]}


Heads in Mathematica can be any expression. Map is doing just as it is instructed.

Perhaps you would like an abstraction along these lines:

deepMap[template_, target_, lev_: {1}] :=
  Map[
    Replace[template &, s_Symbol :> s[#], {-1}, Heads -> False],
    target,
    lev
  ]

Now:

deepMap[-f, {1, 2, 3}]

deepMap[Sin + Cos, {a, b, c}]

deepMap[j^2/k - m, {{1, 2}, {3, 4}}, {2}]
{-f[1], -f[2], -f[3]}

{Cos[a] + Sin[a], Cos[b] + Sin[b], Cos[c] + Sin[c]}

{{j[1]^2/k[1] - m[1], j[2]^2/k[2] - m[2]}, {j[3]^2/k[3] - m[3], j[4]^2/k[4] - m[4]}}

In:

Clear[f]
f[s_][x_] := 2 x s
Map[f[1], Range[10]]
Map[f[-1], Range[10]]

Out:

{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

{-2, -4, -6, -8, -10, -12, -14, -16, -18, -20}

Tags:

Map