25 | Ways to Apply Functions |
When you write
f[x] it means
“apply the function
f to
x”. An alternative way to write the same thing in the Wolfram Language is
f@x.
It
’s often convenient to write out chains of functions using
@:
Avoiding the brackets can make code easier to type, and read:
There
’s a third way to write
f[x] in the Wolfram Language: as an
“afterthought
”, in the form
x//f.
Apply f “as an afterthought” to x:
You can have a sequence of “afterthoughts”:
The functions here read in the order they are applied:
A particularly common use of
// is to apply
N (for numerical evaluation)
“as an afterthought
”.
Apply numerical evaluation “as an afterthought”:
In working with the Wolfram Language, a powerful notation that one ends up using all the time is
/@, which means
“apply to each element
”.
Apply f to each element in a list:
f usually would just get applied to the whole list:
Framed is a function that displays a frame around something.
Display x framed:
Applying
Framed to a list just puts a frame around the whole list.
@ does exactly the same thing:
Now use
/@ to apply
Framed to each element in the list:
The same thing works with any other function. For example, apply the function
Hue separately to each number in a list.
/@ applies
Hue separately to each number in the list:
Here
’s what the
/@ is doing:
It
’s the same story with
Range, though now the output is a list of lists.
/@ applies
Range separately to each number, producing a list of lists:
Here’s the equivalent, all written out:
Given a list of lists,
/@ is what one needs to do an operation separately to each sublist.
Apply
PieChart separately to each list in a list of lists:
You can use exactly the same idea with lots of other functions.
Apply
Length to each element, getting the length of each sublist:
Applying
Length to the whole list just gives the total number of sublists:
Apply
Reverse to each element, getting three different reversed lists:
Apply
Reverse to the whole list, reversing its elements:
As always, the form with brackets is exactly equivalent:
Some calculational functions are
listable, which means they automatically apply themselves to elements in a list.
N is listable, so you don
’t have to use
/@ to get it applied to each element in a list:
The same is true with
Prime:
A function like
Graphics definitely isn
’t listable.
This makes a single graphic with three objects in it:
This gives three separate graphics, with
Graphics applied to each object:
When you enter
f/@{1,2,3}, the Wolfram Language interprets it as
Map[f,{1,2,3}].
f/@x is usually read as
“map
f over
x”.
The internal interpretation of
f/@{1, 2, 3}:
f@x | | equivalent to f[x] |
x//f | | equivalent to f[x] |
f/@{a,b,c} | | apply f separately to each element of the list |
Map[f,{a,b,c}] | | alternative form of /@ |
Framed[expr] | | put a frame around something |
25.1Use
/@ and
Range to reproduce the result of
Table[f[n], {n, 5}].
»
25.2Use
/@ twice to generate
Table[f[g[n]], {n, 10}].
»
25.3Use
// to create
a[b[c[d[x]]]].
»
25.4Make a list of letters of the alphabet, with a frame around each one.
»
25.5Color negate an image of each planet, giving a list of the results.
»
25.6Use
/@ to draw separate maps of each country in the G5.
»
25.7Binarize each flag in Europe, and make an image collage of the result.
»
25.8Find a list of the dominant colors in images of the planets, putting the results for each planet in a column.
»
25.9Find the total of the letter numbers given by
LetterNumber for the letters in the word
“wolfram
”.
»
Why not always use
f@x instead of
f[x]?
f@x is a fine equivalent to
f[x], but the equivalent of
f[1+1] is
f@(1+1), and in that case,
f[1+1] is shorter and easier to understand.
It comes from math. Given a set
{1, 2, 3},
f/@{1, 2, 3} can be thought of as mapping of this set to another one.
How does one say
"//" and
"/@"?
Typically
“slash slash
” and
“slash at
”.
When do I need to use parentheses with
@,
// and
/@?
It
’s determined by the
precedence or
binding of different operators.
@ binds tighter than
+, so
f@1+1 means
f[1]+1 not
f@(1+1) or
f[1+1].
// binds looser than
+, so
1/2+1/3//N means
(1/2+1/3)//N. In a notebook you can find how things are grouped by repeatedly clicking on your input, and seeing how the selection expands.
- Quite a few functions are “listable”, so they automatically map themselves over lists.
- Range is listable, so Range[{3, 4, 5}] is the same as Range/@{3, 4, 5}.