Re-posted from: https://bkamins.github.io/julialang/2024/02/23/fix.html
Introduction
Some functions provided in Base Julia support partial application.
I often find this functionality useful.
Therefore in this post I want to give you its explanation and a summary which functions have this property.
The post was tested with Julia Version 1.12.0-DEV.53.
Explaining partial function application
We will focus on partial application of functions having two positional arguments.
Let us work by example.
Consider the in
function. You can call it to check if some item is in a collection.
Here is an example:
julia> in('a', "Abracadabra")
true
julia> in('x', "Abracadabra")
false
A common pattern you might need is to perform a repeated check if various items are contained in the same collection.
For example assume you have a vector of characters and you want to filer it to keep only the elements contained in a reference collection.
You can do it like this:
julia> v = 'a':'z'
'a':1:'z'
julia> filter(x -> in(x, "Abracadabra"), v)
5-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'r': ASCII/Unicode U+0072 (category Ll: Letter, lowercase)
This pattern is so commonly needed that there is a shorthand for x -> in(x, "Abracadabra")
.
Instead of creating this anonymous function you can just write in("Abracadabra")
.
The value returned by this function call behaves in the same way as x -> in(x, "Abracadabra")
.
Let us check:
julia> filter(in("Abracadabra"), v)
5-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'r': ASCII/Unicode U+0072 (category Ll: Letter, lowercase)
You can think of this operation as if we partially applied the in
function by fixing its second argument
(the collection) and leaving the first (the item we check) to be specified later.
In other words the following two operations are equivalent:
julia> in('a', "Abracadabra")
true
julia> in("Abracadabra")('a')
true
Fixing of the second argument is most common. However, sometimes it is useful to fix the first argument.
This is exactly the case of the filter
function we have just used.
What if you wanted to perform the filter(in("Abracadabra"), v)
test for multiple different values of v
but with a fixed predicate function?
Here is an example:
julia> vv = ['a'+i:'z' for i in 0:4]
5-element Vector{StepRange{Char, Int64}}:
'a':1:'z'
'b':1:'z'
'c':1:'z'
'd':1:'z'
'e':1:'z'
julia> map(v -> filter(in("Abracadabra"), v), vv)
5-element Vector{Vector{Char}}:
['a', 'b', 'c', 'd', 'r']
['b', 'c', 'd', 'r']
['c', 'd', 'r']
['d', 'r']
['r']
You probably see, where I am getting at. Instead of v -> filter(in("Abracadabra"), v)
we can write filter(in("Abracadabra"))
and fix
the first positional argument of filter
, leaving the second to be specified later.
Let us check if this works:
julia> map(filter(in("Abracadabra")), vv)
5-element Vector{Vector{Char}}:
['a', 'b', 'c', 'd', 'r']
['b', 'c', 'd', 'r']
['c', 'd', 'r']
['d', 'r']
['r']
Indeed, we get what we expected. Again, for a reference note that the following two operations are equivalent:
julia> filter(in("Abracadabra"), v)
5-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'r': ASCII/Unicode U+0072 (category Ll: Letter, lowercase)
julia> filter(in("Abracadabra"))(v)
5-element Vector{Char}:
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
'r': ASCII/Unicode U+0072 (category Ll: Letter, lowercase)
Before I finish this section let me note that if you do not like writing that many parentheses you could use the |>
operator.
In our example we could write:
julia> map("Abracadabra" |> in |> filter, vv)
5-element Vector{Vector{Char}}:
['a', 'b', 'c', 'd', 'r']
['b', 'c', 'd', 'r']
['c', 'd', 'r']
['d', 'r']
['r']
Which style you use is a matter of preference.
Catalogue of function supporting partial applications
We saw that some functions taking two arguments support partial application.
Below I give you a list of all of them that are currently supported (and this is the reason why the post is written under Julia nightly,
as there were recent changes in this list).
There is only one function in Base Julia that supports fixing its first argument and this function is filter
.
However, there are many functions supporting fixing of their second argument. Here is their list:
- comparisons:
isequal
,==
,!=
,>=
,<=
,>
,<
; - inclusion testing:
in
,∈
,∋
,∉
,∌
; - string checking:
contains
,occursin
,endswith
,startswith
; - set operations (supported since Julia 1.11; not released yet):
issubset
,⊆
,⊇
,⊈
,⊉
,⊊
,⊋
,isdisjoint
,issetequal
.
Conclusions
After reading this post you know how to use partial function application in Julia and which functions from Base support it.
I hope you will find this functionality useful in your code.