Today we learn Set, an immutable collection that automatically ensures all its elements are unique and sorted
Create Set
Using set notation
printfn “numberSet: %A” numberSet // numberSet: set [1; 2; 3; 4; 5]
from List
printfn “Fruits: %A” fruits
Set.empty create an empty Set
printfn “emptySet: %A” emptySet // emptySet: set []
Get element not support
printfn “Fruits first element: %A” fruits.[0]
raise Error: The type ‘Set<_>’ does not define the field, constructor or member ‘Item’
Add/Remove element
Set.add
Returns a new set with an element added to the set. No exception is raised if the set already contains the given element
printfn $”The new set is: {set}”
Set.remove
Returns a new set with the given element removed. No exception is raised if the set doesn’t contain the given element
printfn $”The set without 1 is {Set.remove 1 set}” // The set without 1 is set [2; 3]
printfn $”The set without 1 is {Set.remove 4 set}” // The set without 1 is set [1; 2; 3]
we can see set itself didn’t change after remove 1
Loop Set
for … in
for fruit in fruits do
printfn “Fruit: %s” fruit
Set.iter
Applies the given function to each element of the set, in order according to the comparison function
Set.iter (fun x -> printfn $”The set contains {x}”) set
Element operation
Contains
syntax: Set.Contains element set
Evaluates to “true” if the given element is in the given set
printfn $”Does the set contain 1? {Set.contains 1 set}” // Does the set contain 1? false
use Instance Member Function
printfn $”Does the set contain 1? {set.Contains(1)}” // Does the set contain 1? false
exists
syntax: Set.exists predicate set
Tests if any element of the collection satisfies the given predicate
printfn $”Does the set contain even number? {Set.exists (fun x -> x % 2 = 0) set}” // Does the set contain even number? true
filter
syntax: Set.filter predicate set
Returns a new collection containing only the elements of the collection for which the given predicate returns True
printfn $”The set with even numbers is {Set.filter (fun x -> x % 2 = 0) set}” // The set with even numbers is set [2; 4]
Set operation
Lucy and Lily having breakfast:
Lucy have milk, boiled egg and sausages, Lily have milk, bacon and fried egg,
we can define their food list by 2 Sets: set [“milk”, “boiled egg”, “sausages”], set [“milk”, “bacon”, “fried egg”]
union
union is all the elements in 2 Sets
let LilyBreakfastSet = set [“milk”; “bacon”; “fried egg”]
printfn $”The union of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.union LucyBreakfastSet LilyBreakfastSet)}”
the union is expected to be “milk”, “boiled egg”, “sausages”, “bacon”, “fried egg”
results: The union of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [bacon; boiled egg; fried egg; milk; sausages]
intersect
Computes the intersection of the two sets, the common elements they share
let LilyBreakfastSet = set [“milk”; “bacon”; “fried egg”]
printfn $”The intersect of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.intersect LucyBreakfastSet LilyBreakfastSet)}”
the intersect is expected to be “milk”
results: The intersect of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [milk]
difference
difference returns a new set with the elements of the second set removed from the first set, which is the left colored part in the picture (A left B right)
if we put Lucy’s breakfast in the first place
let LilyBreakfastSet = set [“milk”; “bacon”; “fried egg”]
printfn $”The difference of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.difference LucyBreakfastSet LilyBreakfastSet)}”
we got “boiled egg”; “sausages”
result: The difference of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [boiled egg; sausages]
Set.difference LucyBreakfastSet LilyBreakfastSet is equivalent to LucyBreakfastSet – LilyBreakfastSet
we can understand like this: remove the food Lily also had in Lucy’s food
let LilyBreakfastSet = set [“milk”; “bacon”; “fried egg”]
printfn $”The difference of {LucyBreakfastSet} and {LilyBreakfastSet} is {(LucyBreakfastSet – LilyBreakfastSet)}”