26 | Pure Anonymous Functions |
After all the examples we
’ve seen of the Wolfram Language, we
’re now ready to go to a slightly higher level of abstraction, and tackle the very important concept of
pure functions (also known as
pure anonymous functions).
Using pure functions will let us unlock a new level of power in the Wolfram Language, and also let us redo some of the things we’ve done before in a simpler and more elegant way.
Let
’s start with a simple example. Say we
’ve got a list of images, and we want to apply
Blur to each of them. That
’s easy to do with
/@.
Apply
Blur to each image in the list:
But now let
’s say we want to include the parameter
5 in
Blur. How can we do that? The answer is to use a pure function.
Include a parameter by introducing a pure function:
The original blur written as a pure function:
The
# is a
“slot
” into which each element is put. The
& says that what comes before it is a pure function.
Here
’s the equivalent of
Blur[#, 5]&/@... expanded out:
Let
’s look at some other examples. Every time, the slot indicates where to put each element when the pure function is applied.
Rotate each string by 90
°:
Take a string and rotate it different amounts:
Show text in a list of different colors:
Make circles different sizes:
Show framed columns of a color and its negation:
Compute the lengths of three Wikipedia articles:
Pair topics with results:
Make a grid of everything:
This makes a list of digits, then maps a pure function over it:
Here’s what the pure function would do if mapped over {6, 8, 9}:
Now that we’ve seen some examples of pure functions in action, let’s look more abstractly at what’s going on.
This maps an abstract pure function over a list:
Here’s the minimal example:
It’s equivalent to:
We can put slots wherever we want in the pure function, as many times as we want. All the slots will get filled with whatever the pure function is applied to.
Apply a slightly more complicated pure function:
It
’s easier to read in a column:
OK, now we
’re ready to finally discuss how pure functions really work. When we write
f[x], we
’re applying the function
f to
x. Often we
’ll use a specific named function instead of
f, say
Blur, so we have
Blur[x], etc.
But the point is that we can also replace f with a pure function. Then whatever we apply this to will be used to fill the slot in the pure function.
Apply a pure function to
x, so the
# slot gets filled with
x:
An equivalent form, written with
@ instead of
[...]:
So now we can see what
/@ is doing: it
’s just applying the pure function to each element in the list.
The same thing, written out more explicitly:
Why is this useful? First of all, because it
’s the foundation for all the things pure functions do with
/@. But it
’s actually also often useful on its own, for example as a way to avoid having to repeat things.
Here
’s an example of a pure function involving three occurrences of
#.
This is what it looks like without the pure function:
In the Wolfram Language, a pure function works just like anything else. On its own, though, it doesn’t do anything.
Enter a pure function on its own and it’ll come back unchanged:
Give it to the function
Map (
/@), though, and it
’ll be used to do a computation.
Map uses the pure function to do a computation:
Over the course of the next few sections, we’ll see more and more uses of pure functions.
code& | | a pure function |
# | | slot in a pure function |
26.1Use
Range and a pure function to create a list of the first 20 squares.
»
26.2Make a list of the result of blending yellow, green and blue with red.
»
26.3Generate a list of framed columns containing the uppercase and lowercase versions of each letter of the alphabet.
»
26.4Make a list of letters of the alphabet, in random colors, with frames having random background colors.
»
26.5Make a table of G5 countries, together with their flags, and arrange the result in a fully framed grid.
»
26.6Make a list of word clouds for the Wikipedia articles about apple, peach and pear.
»
26.7Make a list of histograms of the word lengths in Wikipedia articles on apple, peach and pear.
»
26.8Make a list of maps of Central America, highlighting each country in turn.
»
+26.1Give a simpler form for
(#^2+1&)/@Range[10].
»
Why are they called “pure functions”?
Because all they do is serve as functions that can be applied to arguments. They
’re also sometimes called
anonymous functions, because, unlike say
Blur, they
’re not referred to by a name. Here I
’m calling them
“pure anonymous functions
” to communicate both meanings.
The
& (
ampersand) indicates that what comes before it is the
“body
” of a pure function, not the name of a function.
f/@{1, 2, 3} gives
{f[1], f[2], f[3]}, but
f&/@{1, 2, 3} gives
{f, f, f}.
What is
f[#, 1]& interpreted as?
- Pure functions are a characteristic feature of functional programming. They’re often called lambda expressions, after their use in mathematical logic in the 1930s. Confusingly, the term “pure function” sometimes just means a function that has no side effects (i.e. assigns no values to variables, etc.)
- Table[f[x], {x, {a, b, c}}] actually does the same as f/@{a, b, c}. It’s sometimes useful, particularly if one doesn’t want to have to explain pure functions.
- Be careful if you have multiple nested &’s in an expression! Sometimes you may have to insert parentheses. And sometimes you may have to use Function with a named variable, as in Function[x, x^2] rather than #^2&, to avoid conflicts between uses of # in different functions.
- It sometimes makes for good-looking code to write Function[x, x^2] as xx^2. The can be typed as \ [Function] or fn. A form like x x^2 coincides with the standard mathematical notation for “x is mapped to x^2” or “x becomes x^2”.
- Options can often be pure functions. It’s important to put parentheses around the whole pure function, as in ColorFunction(Hue[#/4]&), or it won’t be interpreted as you expect.