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 /@.

Blur/@{,,}

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.

Blur[#,5]&/@{,,}

Blur[#]&/@{,,}

The # is a “slot” into which each element is put. The & says that what comes before it is a pure function.

{Blur[,5],Blur[,5],Blur[,5]}

Let’s look at some other examples. Every time, the slot indicates where to put each element when the pure function is applied.

Rotate[#,90Degree]&/@{"one","two","three"}

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"}

{#,StringLength[WikipediaData[#]]}&/@{"apple","peach","pear"}

Grid[{#,StringLength[WikipediaData[#]]}&/@{"apple","peach","pear"}]

Style[#,Hue[#/10],5*#]&/@IntegerDigits[2^100]

{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.

f[#,x]&/@{a,b,c,d,e}

f[#]&/@{a,b,c,d,e}

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.

f[#,{x,#},{#,#}]&/@{a,b,c}

f[#,{x,#},{#,#}]&/@{a,b,c}//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.

f[#,a]&  [x]

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}

{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 #.

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.

f[#,2]&

Give it to the function Map (/@), though, and it’ll be used 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.

Use Range and a pure function to create a list of the first 20 squares. »

Make a list of the result of blending yellow, green and blue with red. »

Generate a list of framed columns containing the uppercase and lowercase versions of each letter of the alphabet. »

Make a list of letters of the alphabet, in random colors, with frames having random background colors. »

Make a table of G5 countries, together with their flags, and arrange the result in a fully framed grid. »

Make a list of word clouds for the Wikipedia articles about apple, peach and pear. »

Make a list of histograms of the word lengths in Wikipedia articles on apple, peach and pear. »

Make a list of maps of Central America, highlighting each country in turn. »

Give 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.

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”.

Tech Notes