F# For Dummys - Day 15 Collections Set

45 阅读1分钟

Today we learn Set, an immutable collection that automatically ensures all its elements are unique and sorted

Create Set

  • Using set notation
let numberSet = set [1; 3; 5; 4; 2; 3]
printfn "numberSet: %A" numberSet // numberSet: set [1; 2; 3; 4; 5]
  • from List
let fruits = Set.ofList ["apple"; "banana"; "orange"; "apple"]
printfn "Fruits: %A" fruits
  • Set.empty create an empty Set
let emptySet = Set.empty
printfn "emptySet: %A" emptySet // emptySet: set []

Get element not support

let fruits = Set.ofList ["apple"; "banana"; "orange"]
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
let set = Set.empty.Add(1).Add(1).Add(2)
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
let set = Set.empty.Add(1).Add(2).Add(3)
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
let fruits = Set.ofList ["apple"; "banana"; "orange"; "grape"]

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
let set = Set.empty.Add(1).Add(2).Add(3)
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
let set = Set.empty.Add(2).Add(3)
printfn $"Does the set contain 1? {Set.contains 1 set}" // Does the set contain 1? false

use Instance Member Function

let set = Set.empty.Add(2).Add(3)
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
let set = Set.empty.Add(1).Add(2).Add(3)
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
let set = Set.empty.Add(1).Add(2).Add(3).Add(4)
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

image.png Lucy and Lily having breakfast:
Lucy have milk, boiled egg and sausages, Lily have milk, bacon and fried egg,
we can define their food by 2 Set: set ["milk", "boiled egg", "sausages"], set ["milk", "bacon", "fried egg"]

  • union
    union is all the elements in 2 sets
let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
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 LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
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)

image.png

if we put Lucy's breakfast in the first place

let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
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 LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
let LilyBreakfastSet = set ["milk"; "bacon"; "fried egg"]
printfn $"The difference of {LucyBreakfastSet} and {LilyBreakfastSet} is {(LucyBreakfastSet - LilyBreakfastSet)}"