11
$\begingroup$

Regarding:

Map[#[{2, b}] &, {Reverse}]

The above works, but it makes my head spin. The documentation for Map says that the first entry is the function, and yet the function of interest (i.e. Reverse) is really inside the second entry.

The first entry #[{2, b}] & is a function because it ends in a &, but the argument of this function is the name of that function (again, Reverse).

Can anybody help me put think about this in a simple way?

$\endgroup$
3
  • 1
    $\begingroup$ Reverse is a function that is being passed as a parameter value to the pure function #[{2,b}]&. The # (really Slot[1]) will be replaced by the 1st parameter so the pure function becomes Reverse[[{2,b}] and then is evaluated. Functions can be parameters of functions. $\endgroup$
    – Edmund
    Commented Apr 22, 2023 at 17:17
  • 1
    $\begingroup$ Or perhaps Edmund you should say that the name of a function can be a parameter of a function?? $\endgroup$
    – Chris
    Commented Apr 22, 2023 at 17:21
  • 1
    $\begingroup$ Its not the "name" of the function as it can be a variable assigned to that function. Take foo = Reverse and then replace Reverse with foo in your original expression. $\endgroup$
    – Edmund
    Commented Apr 22, 2023 at 20:54

2 Answers 2

20
$\begingroup$

A function is really a mathematical idea. In Mathematica, we can represent, or denote, a function in a few ways. But Mathematica is really working with expressions.

Map[a, {b, c, d}]

{a[b], a[c], a[d]}

Mathematica doesn't know that this is meaningless. It's just applying its rules for transforming expressions.

Map[#[x] &, {a, b, c}]

{a[x], b[x], c[x]}

The way Function works (the # and & are just syntactic sugar for Function), is to just replace the argument slots with the values passed in as arguments. And Map does this for every element in a list (well, more accurately, every element at the first level of an expression).

Map[#[x] &, {Sin, Cos, Tan}]

{Sin[x], Cos[x], Tan[x]}

Sin, Cos, and Tan aren't functions, they're just symbols to which evaluation rules are attached. In this case, there are no rules that take us further than just Sin[x] et al, so it stops there and that's the result.

Map[#[{2, b}] &, {Reverse}]

If we could pause evaluation, we could see this: {Reverse[{2, b}]}. But there are rules that still apply to this expression, so we continue on, eventually arriving at {{b, 2}}.

Can anybody help me put think about this in a simple way?

I don't know if you'll find it simple, but try to remember that we apply the concept function to our expressions, but expressions aren't inherently functions. Functions are abstract, but we want to compute as if they're real, so we create expressions that work as if they were functions. This is very useful, but it's just pretending.

$\endgroup$
2
  • 2
    $\begingroup$ beautifully explained. $\endgroup$
    – alex
    Commented Apr 23, 2023 at 12:56
  • $\begingroup$ (++1) Nice, @lericr! $\endgroup$ Commented Apr 23, 2023 at 13:18
11
$\begingroup$
Clear["Global`*"]

Map[#[{2, b}] &, {Reverse}]

(* {{b, 2}} *)

Or equivalently,

#[{2, b}] & /@ {Reverse}

(* {{b, 2}} *)

While Reverse is a function, in this context the function for Map is #[{2, b}]&. This construct is useful when there are a number of functions to be used and you don't want to repeat the common argument. For example,

#[ExponentialDistribution[λ]] & /@ {Mean, Variance, Median, Skewness, 
  Kurtosis}

(* {1/λ, 1/λ^2, Log[2]/λ, 2, 9} *)

The function being mapped is #[ExponentialDistribution[λ]]& onto the list of functions {Mean, Variance, Median, Skewness, Kurtosis}

EDIT: The functions being mapped onto need not be only single argument functions.

#[ExponentialDistribution[λ]] & /@ {Mean, Variance, Median, Skewness, 
  Kurtosis, Moment[#, r] &, CentralMoment[#, r] &}

(* {1/λ, 1/λ^2, Log[2]/λ, 2, 9, λ^-r r!, λ^-r Subfactorial[r]} *)
$\endgroup$

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.