We can define a function to perform function composition using anonymous function syntax:
f ∘ g = x -> f(g(x))
Note that this definition is equivalent to each of the following definitions:
∘(f, g) = x -> f(g(x))
or
function ∘(f, g)
x -> f(g(x))
end
recalling that in Julia, f ∘ g
is just syntax sugar for ∘(f, g)
.
We can see that this function composes correctly:
julia> double(x) = 2x
double (generic function with 1 method)
julia> triple(x) = 3x
triple (generic function with 1 method)
julia> const sextuple = double ∘ triple
(::#17) (generic function with 1 method)
julia> sextuple(1.5)
9.0
In version v0.5, this definition is very performant. We can look into the LLVM code generated:
julia> @code_llvm sextuple(1)
define i64 @"julia_#17_71238"(i64) #0 {
top:
%1 = mul i64 %0, 6
ret i64 %1
}
It is clear that the two multiplications have been folded into a single multiplication, and that this function is as efficient as is possible.
How does this higher-order function work? It creates a so-called closure, which consists of not just its code, but also keeps track of certain variables from its scope. All functions in Julia that are not created at top-level scope are closures.
One can inspect the variables closed over through the fields of the closure. For instance, we see that:
julia> (sin ∘ cos).f
sin (generic function with 10 methods)
julia> (sin ∘ cos).g
cos (generic function with 10 methods)