Sets are useful concepts in Julia but few people uses them so in this article we shall describe the basic concept of Sets and how to use them
Construction
julia> evenunderten = Set([2,4,6,8])
Set{Int64} with 4 elements:
4
6
2
8
julia> oddunderten = Set([1,3,5,7,9])
Set{Int64} with 5 elements:
5
7
9
3
1
You can use basic Set operators
julia> numbersunderten = union(evenunderten,oddunderten)
Set{Int64} with 9 elements:
5
4
6
7
2
9
8
3
1
julia> issubset(evenunderten,numbersunderten)
true
julia> bothevenandoddunderten = intersect(evenunderten,oddunderten)
Set{Int64}()
julia> primesunderten = Set([2,3,5,7])
Set{Int64} with 4 elements:
5
7
2
3
julia> evenandprime = intersect(evenunderten,primesunderten)
Set{Int64} with 1 element:
2
julia> nonprimeevens = setdiff(evenunderten,primesunderten)
Set{Int64} with 3 elements:
4
6
8
Get the size of an existing Set
julia> length(numbersunderten)
9
Checking if a set contains a specific element
julia> 16 in numbersunderten
false
julia> 6 in numbersunderten
true
Adding an element to an existing Set
julia> push!(evenunderten,0)
Set{Int64} with 5 elements:
0
4
6
2
8
julia> evenunderten
Set{Int64} with 5 elements:
0
4
6
2
8
Deleting a specific element from an existing Set
julia> delete!(evenunderten,0)
Set{Int64} with 4 elements:
4
6
2
8
julia> evenunderten
Set{Int64} with 4 elements:
4
6
2
8
Get all elements of a Set
@inline function elementsofset(S::Set)
return [ element for element in S ]
end
julia> elementsofset(evenunderten)
4-element Vector{Int64}:
4
6
2
8
Pop out the smallest element of a set
function popsmallest!(S::Set)
if length(S) == 0
return nothing
end
local smallest = minimum(elementsofset(S))
delete!(S,smallest)
return smallest
end
julia> popsmallest!(evenunderten)
2
julia> evenunderten
Set{Int64} with 3 elements:
4
6
8
Find all even elements of a set
julia> filter(iseven,elementsofset(numbersunderten))
4-element Vector{Int64}:
4
6
2
8
Find all odd elements of a set
julia> filter(!iseven,elementsofset(numbersunderten))
5-element Vector{Int64}:
5
7
9
3
1
In Statistics you can do sampling without replacement, here we can do it with Sets
julia> function samplingwithoutreplacement(S::Set)
local sample = rand(S)
delete!(S,sample)
return sample
end
samplingwithoutreplacement (generic function with 1 method)
julia> a = samplingwithoutreplacement(evenunderten)
4
julia> b = samplingwithoutreplacement(evenunderten)
6
julia> evenunderten
Set{Int64} with 1 element:
8
We can use this to deal out 4 cards out of a pack of 52 playing cards
julia> playingcardset = Set( reduce(vcat, [ "$(k) of $(suite)" for k = 1:13 , suite = ["Diamond","Heart","Spade","Club"] ] ) )
Set{String} with 52 elements:
"2 of Club"
"1 of Diamond"
"5 of Club"
"6 of Spade"
"4 of Club"
"7 of Heart"
"2 of Diamond"
"8 of Diamond"
"10 of Diamond"
"2 of Spade"
"11 of Club"
"13 of Diamond"
"5 of Diamond"
â‹®
julia> firstcard = samplingwithoutreplacement(playingcardset)
"1 of Diamond"
julia> secondcard = samplingwithoutreplacement(playingcardset)
"6 of Heart"
julia> thirdcard = samplingwithoutreplacement(playingcardset)
"6 of Spade"
julia> fourthcard = samplingwithoutreplacement(playingcardset)
"4 of Spade"
Now we can simulate Adam and Eve playing poker.
julia> adam = Set([])
Set{Any}()
julia> eve = Set([])
Set{Any}()
julia> playingcardset = Set( reduce(vcat, [ "$(k) of $(suite)" for k = 1:13 , suite = ["Diamond","Heart","Spade","Club"] ] ) )
Set{String} with 52 elements:
"2 of Club"
"1 of Diamond"
"5 of Club"
"6 of Spade"
"4 of Club"
"7 of Heart"
"2 of Diamond"
"8 of Diamond"
"10 of Diamond"
"2 of Spade"
"11 of Club"
"13 of Diamond"
"5 of Diamond"
â‹®
julia> for card = 1:5
push!(adam,samplingwithoutreplacement(playingcardset))
push!(eve,samplingwithoutreplacement(playingcardset))
end
julia> adam
Set{Any} with 5 elements:
"6 of Club"
"9 of Heart"
"5 of Heart"
"12 of Club"
"9 of Club"
julia> eve
Set{Any} with 5 elements:
"2 of Spade"
"10 of Spade"
"6 of Heart"
"1 of Club"
"7 of Heart"
Top comments (4)
Hi, I am not a user of
Set
s but I'm wondering, all of what you described can be achieved by usingVector
s, what is the difference then ?All of what I describe can be done with vectors but I am showing you how to do it with Sets. The reason is that you can use Mathematical logic to deal with complex ideas using Sets.
Just to make it practical, do you have an example of such idea ?
Just as an additional reference about Sets vs Vectors juliabloggers.com/set-vs-vector-lo...
Some comments may only be visible to logged-in visitors. Sign in to view all comments.