Explore the latest version of An Elementary Introduction to the Wolfram Language »
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 weve done before in a simpler and more elegant way.
Apply Blur to each image in the list:
In[1]:=
Click for copyable input
Out[1]=
But now lets 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:
In[2]:=
Click for copyable input
Out[2]=
The original blur written as a pure function:
In[3]:=
Click for copyable input
Out[3]=
The # is a slot into which each element is put. The & says that what comes before it is a pure function.
Heres the equivalent of Blur[#, 5]&/@... expanded out:
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
Take a string and rotate it different amounts:
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=
In[10]:=
Click for copyable input
Out[10]=
Pair topics with results:
In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
Out[12]=
In[13]:=
Click for copyable input
Out[13]=
Heres what the pure function would do if mapped over {6, 8, 9}:
In[14]:=
Click for copyable input
Out[14]=
Now that weve seen some examples of pure functions in action, lets look more abstractly at whats going on.
This maps an abstract pure function over a list:
In[15]:=
Click for copyable input
Out[15]=
Heres the minimal example:
In[16]:=
Click for copyable input
Out[16]=
Its equivalent to:
In[17]:=
Click for copyable input
Out[17]=
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:
In[18]:=
Click for copyable input
Out[18]=
In[19]:=
Click for copyable input
Out[19]=
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:
In[20]:=
Click for copyable input
Out[20]=
An equivalent form, written with @ instead of [...]:
In[21]:=
Click for copyable input
Out[21]=
So now we can see what /@ is doing: its just applying the pure function to each element in the list.
In[22]:=
Click for copyable input
Out[22]=
The same thing, written out more explicitly:
In[23]:=
Click for copyable input
Out[23]=
Why is this useful? First of all, because its the foundation for all the things pure functions do with /@. But its actually also often useful on its own, for example as a way to avoid having to repeat things.
Heres an example of a pure function involving three occurrences of #.
Apply a pure function to Blend[{Red, Yellow}]:
In[24]:=
Click for copyable input
Out[24]=
In[25]:=
Click for copyable input
Out[25]=
In the Wolfram Language, a pure function works just like anything else. On its own, though, it doesnt do anything.
Enter a pure function on its own and itll come back unchanged:
In[26]:=
Click for copyable input
Out[26]=
Give it to the function Map (/@), though, and itll be used to do a computation.
Map uses the pure function to do a computation:
In[27]:=
Click for copyable input
Out[27]=
Over the course of the next few sections, well 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. Theyre also sometimes called anonymous functions, because, unlike say Blur, theyre not referred to by a name. Here Im 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.
 
Download Notebook Version
es