Wolfram Language Fast Introduction for Programmers
Get Started »

Applying Functions

Its very common to want to map a function over multiple expressions:

In[1]:=
X
Out[1]=

/@ ( slash at) is a short notation for Map:

In[2]:=
X
Out[2]=

Map in the Wolfram Language works similarly to the Stream.map method in Java, except that Map can be applied to any kind of expression.

Map works the same in the Wolfram Language as map does in Python. Apply is similar to using the unpacking star * operator in Python.

This uses a pure function:

In[3]:=
X
Out[3]=

Apply applies a function to multiple arguments:

In[1]:=
X
Out[1]=

Expressions have levels—corresponding to the number of indices needed to extract a part.
Functions like Map can operate at specific levels.

"Levels" are another name for dimensions of an array, but generalized for all symbolic expressions. Multi-dimensional operations like this are not built into Java and would normally require loops.

"Levels" are another name for dimensions of an array, but generalized for all symbolic expressions. Most of Python's array functions do not offer the same flexibility for operating on different dimensions.

Map defaults to operate at level 1:

In[1]:=
X
Out[1]=

This operates only at level 2:

In[2]:=
X
Out[2]=

@@ is equivalent to Apply, operating by default at level 0:

In[1]:=
X
Out[1]=

@@@ means apply at level 1:

In[2]:=
X
Out[2]=

@ means ordinary function application:

In[3]:=
X
Out[3]=
Check Your Understanding

Which expression evaluates to g[{a, b, c}]?

g@{a, b, c}

Correct.

g /@ {a, b, c}

Incorrect. This gives {g[a], g[b], g[c]}.

g @@ {a, b, c}

Incorrect. This gives g[a, b, c].

Which one of these expressions evaluates to {{f[a], f[b]}, {f[c], f[d]}}?

f @@ {{a, b}, {c, d}}

Incorrect. @@ is the shortcut for Apply, which by default operates at level 0 and would evaluate to f[{a, b}, {c, d}].

f @@@ {{a, b}, {c, d}}

Incorrect. @@@ is the shortcut for Apply at level 1, which would evaluate to {f[a, b], f[c, d]}.

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

Correct.

Which of the following evaluates to {{{f[a], f[b]}}, {{f[c], f[d]}}}?

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

Incorrect. Map defaults to level 1, and requires a level specification to act at level 3.

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

Correct.

f @@@ {{{a, b}}, {{c, d}}}

Incorrect. This is the shortcut for Apply at level 1.