logo

Sets and Operators in Julia 📂Julia

Sets and Operators in Julia

Overview

Julia, like Python, supports the set data type. As with any set data type, it is incredibly useful for those who use it and utterly ignored by those who don’t. Given that Julia’s design is closely aligned with mathematics, its implementation of set concepts and operations is robust, making it an important feature to understand. Honeycam2019-12-1113-12-52.gif Perhaps the most distinct difference from other languages, especially Python, is the ability to use Unicode symbols as part of the code. If you are using Juno in the Atom editor, you can autocomplete such TeX codes as shown above. In this context, $\in$ is not merely a symbol but actually describes whether an element belongs to a set.

Code

julia> X = Set([1,2,3,1]); print(X)
Set([2, 3, 1])
julia> X[1]
ERROR: MethodError: no method matching getindex(::Set{Int64}, ::Int64)
Stacktrace:
 [1] top-level scope at REPL[23]:1

julia> for i in X print(i) end
231

The code above means to define a set $X$ as $X := \left\{ 1, 2, 3, 1 \right\} = \left\{ 2,3,1 \right\}$. As in mathematics, the concept of duplicates and order does not exist. Therefore, referencing the first index will result in an error. However, the data type is still iterable like in Python, meaning it can be used in loops.

julia> if 1∈X print("!") else print("?") end
!
julia> if 0∈X print("!") else print("?") end
?
julia> if 0∉X print("!") else print("?") end
!
julia> if [1,2] ⊆ X print("!") else print("?") end
!
julia> if [0,1,2] ⊆ X print("!") else print("?") end
?

If this feels like reading an equation, you are ready to use the set data type effectively. It is important to note that such operations are not exclusive to the set data type; they are equally applicable to lists. Even if the set data type feels unfamiliar, as long as you are comfortable with sets, using Julia’s set operators should pose no problem. Note that the relation of containment should be written as \subseteq $\subseteq$, not \subset $\subset$.

julia> Y = ["1","2",3]
3-element Array{Any,1}:
  "1"
  "2"
 3

julia> ∪(X,Y)
Set{Any} with 5 elements:
  "1"
  2
  3
  "2"
  1

julia> ∩(X,Y)
Set{Int64} with 1 element:
  3

julia> ∩(Y,X)
1-element Array{Any,1}:
 3

julia> setdiff(X,Y); X
Set{Int64} with 3 elements:
  2
  3
  1

julia> setdiff!(X,Y); X
Set{Int64} with 2 elements:
  2
  1

Naturally, since it deals with sets, both union and intersection can be expressed similarly to mathematical equations. The difference between the second and third lines is the order of operations. $X$ is a set data type in Julia, while $Y$ is defined as an array, and the returned value follows the data type of the first argument. This distinction is crucial in a strongly typed language like Julia and must be thoroughly understood. The difference function is defined in two ways: setdiff() simply returns the set difference, while setdiff!() updates the set itself.

Environment

  • OS: Windows
  • julia: v1.5.0