Julia Community 🟣

Steven Siew
Steven Siew

Posted on • Updated on

Sets under Julia

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
julia> nonprimeevens = setdiff(evenunderten,primesunderten)
Set{Int64} with 3 elements:
  4
  6
  8
Enter fullscreen mode Exit fullscreen mode

Get the size of an existing Set

julia> length(numbersunderten)
9
Enter fullscreen mode Exit fullscreen mode

Checking if a set contains a specific element

julia> 16 in numbersunderten
false

julia> 6 in numbersunderten
true
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Find all even elements of a set

julia> filter(iseven,elementsofset(numbersunderten))
4-element Vector{Int64}:
 4
 6
 2
 8
Enter fullscreen mode Exit fullscreen mode

Find all odd elements of a set

julia> filter(!iseven,elementsofset(numbersunderten))
5-element Vector{Int64}:
 5
 7
 9
 3
 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
bambooxx profile image
BambOoxX

Hi, I am not a user of Sets but I'm wondering, all of what you described can be achieved by using Vectors, what is the difference then ?

Collapse
 
ohanian profile image
Steven Siew

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.

Collapse
 
bambooxx profile image
BambOoxX

Just to make it practical, do you have an example of such idea ?

Thread Thread
 
bambooxx profile image
BambOoxX

Just as an additional reference about Sets vs Vectors juliabloggers.com/set-vs-vector-lo...