Operators and functions

The syntax func.(collection) means applying func to the collection. For example

julia> (x -> 2x).(1:5)
5-element Vector{Int64}:
  2
  4
  6
  8
 10

In your example, ==(2), ≠(2) and >(2) are functions whereas +(2), *(2) and ^(2) are not.

julia> ==(2) isa Function
true

julia> +(2) isa Function
false

For more information, ==(2) is called a partial function application. This is a term from functional programming which means to fix an argument to a function. Specifically, ==(2) is equivalent to defining ==(x) = y -> x == y which is equivalent to Base.Fix2(==, 2). See the documentation of Base.Fix2 for more information about that function.

For readers who wonder why partial function applications are useful, they are mostly useful in combination with higher order functions, that is, functions which take functions such as map or filter. For example, compare

julia> map(x -> x < 2, 1:5)
5-element Vector{Bool}:
 1
 0
 0
 0
 0

julia> filter(x -> x == 2, 1:5)
1-element Vector{Int64}:
 2

julia> filter(s -> contains(s, "ar"), ["foo", "bar"])
1-element Vector{String}:
 "bar"

to

julia> map(<(2), 1:5)
5-element Vector{Bool}:
 1
 0
 0
 0
 0

julia> filter(==(2), 1:5)
1-element Vector{Int64}:
 2

julia> filter(contains("ar"), ["foo", "bar"])
1-element Vector{String}:
 "bar"
3 Likes