Wolfram Computation Meets Knowledge

26 Pure Anonymous Functions

26Pure 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.
Apply Blur to each image in the list:
Blur /@ {\!\(\* GraphicsBox[ {RGBColor[1, 0, 0], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\), \!\(\* GraphicsBox[ {RGBColor[0, 1, 0], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\), \!\(\* GraphicsBox[ {RGBColor[0, 0, 1], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\)}
 
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:
Blur[#, 5] & /@ {\!\(\* GraphicsBox[ {RGBColor[1, 0, 0], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\), \!\(\* GraphicsBox[ {RGBColor[0, 1, 0], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\), \!\(\* GraphicsBox[ {RGBColor[0, 0, 1], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\)}
 
The original blur written as a pure function:
Blur[#] & /@ {\!\(\* GraphicsBox[ {RGBColor[1, 0, 0], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\), \!\(\* GraphicsBox[ {RGBColor[0, 1, 0], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\), \!\(\* GraphicsBox[ {RGBColor[0, 0, 1], CircleBox[{0, 0}]}, ImageSize->{35, 35}]\)}
 
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:
{Blur[\!\(\* GraphicsBox[ {RGBColor[1, 0, 0], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\), 5], Blur[\!\(\* GraphicsBox[ {RGBColor[0, 1, 0], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\), 5], Blur[\!\(\* GraphicsBox[ {RGBColor[0, 0, 1], CircleBox[{0, 0}]}, ImagePadding->3, ImageSize->{40, 40}]\), 5]}
 
Rotate[#, 90 Degree] & /@ {"one", "two", "three"}
 
Take a string and rotate it different amounts:
Rotate["hello", #] & /@ {30 °, 90 °, 180 °, 220 °}
 
Style["hello", 20, #] & /@ {Red, Orange, Blue, Purple}
 
Graphics[Circle[], ImageSize -> #] & /@ {20, 40, 30, 50, 10}
 
Framed[Column[{#, ColorNegate[#]}]] & /@ {Red, Green, Blue, Purple, Orange}
 
StringLength[WikipediaData[#]] & /@ {"apple", "peach", "pear"}
 
Pair topics with results:
{#, StringLength[WikipediaData[#]]} & /@ {"apple", "peach", "pear"}
 
Grid[{#, StringLength[WikipediaData[#]]} & /@ {"apple", "peach", "pear"}]
 
Style[#, Hue[#/10], 5*#] & /@ IntegerDigits[2^100]
 
Here’s what the pure function would do if mapped over {6, 8, 9}:
{Style[6, Hue[6/10], 5*6], Style[8, Hue[8/10], 5*8], Style[9, Hue[9/10], 5*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:
f[#, x] & /@ {a, b, c, d, e}
 
Here’s the minimal example:
f[#] & /@ {a, b, c, d, e}
 
It’s equivalent to:
f /@ {a, b, c, d, e}
 
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:
f[#, {x, #}, {#, #}] & /@ {a, b, c}
 
f[#, {x, #}, {#, #}] & /@ {a, b, c} // Column
 
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:
f[#, a] & [x]
 
An equivalent form, written with @ instead of [...]:
f[#, a] & @ x
 
So now we can see what /@ is doing: it’s just applying the pure function to each element in the list.
f[#, a] & /@ {x, y, z}
 
The same thing, written out more explicitly:
{f[#, a] & @ x, f[#, a] & @ y, f[#, a] & @ z}
 
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 #.
Apply a pure function to Blend[{Red, Yellow}]:
Column[{#, ColorNegate[#], #}] & [Blend[{Red, Yellow}]]
 
Column[{Blend[{Red, Yellow}], ColorNegate[Blend[{Red, Yellow}]], Blend[{Red, Yellow}]}]
 
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:
f[#, 2] &
 
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:
Map[f[#, 2] &, {a, b, c, d, e}]
 
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. »
Expected output:
Out[]=
26.2Make a list of the result of blending yellow, green and blue with red. »
Expected output:
Out[]=
26.3Generate a list of framed columns containing the uppercase and lowercase versions of each letter of the alphabet. »
Expected output:
Out[]=
26.4Make a list of letters of the alphabet, in random colors, with frames having random background colors. »
Sample expected output:
Out[]=
26.5Make a table of G5 countries, together with their flags, and arrange the result in a fully framed grid. »
Expected output:
Out[]=
26.6Make a list of word clouds for the Wikipedia articles about apple, peach and pear. »
Sample expected output:
Out[]=
26.7Make a list of histograms of the word lengths in Wikipedia articles on apple, peach and pear. »
Sample expected output:
Out[]=
Expected output:
Out[]=
+26.1Give a simpler form for (#^2+1&)/@Range[10]»
Expected output:
Out[]=
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.
Why does one need the &?
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?
Function[f[#, 1]]. The Function here is sometimes called the “function function”.
Next Section