Contains operations for working with values of type Set.
Function or value | Description |
|
![]() ![]() ![]() ![]() ![]() ![]() Returns a new set with an element added to the set. No exception is raised if the set already contains the given element.
Example
let set = Set.empty.Add(1).Add(1).Add(2) printfn $"The new set is: {set}"The sample evaluates to the following output: The new set is: set [1; 2]
|
|
Example
let set = Set.empty.Add(2).Add(3) printfn $"Does the set contain 1? {set.Contains(1))}"The sample evaluates to the following output: Does the set contain 1? false
|
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The set has {set.Count} elements"The sample evaluates to the following output: The set has 3 elements
|
|
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(2).Add(3).Add(4) printfn $"The difference of {set1} and {set2} is {Set.difference set1 set2}"The sample evaluates to the following output: The difference of set [1; 2; 3] and set [2; 3; 4] is set [1]
|
Example
Set.empty<int>Evaluates to set [ ] .
|
|
|
![]() ![]() ![]() ![]() ![]() ![]()
Tests if any element of the collection satisfies the given predicate.
If the input function is
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"Does the set contain 1? {Set.exists (fun x -> x = 1) set}"The sample evaluates to the following output: Does the set contain 1? true
|
|
![]() ![]() ![]() ![]() ![]() ![]() Returns a new collection containing only the elements of the collection for which the given predicate returns True.
Example
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 sample evaluates to the following output: The set with even numbers is set [2; 4]
|
Full Usage:
Set.fold folder state set
Parameters:
'State -> 'T -> 'State
-
The accumulating function.
state : 'State
-
The initial state.
set : Set<'T>
-
The input set.
Returns: 'State
The final state.
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The sum of the set is {Set.fold (+) 0 set}" printfn $"The product of the set is {Set.fold (*) 1 set}" printfn $"The reverse of the set is {Set.fold (fun x y -> y :: x) [] set}"The sample evaluates to the following output: The sum of the set is 6
The product of the set is 6
The reverse of the set is [3; 2; 1]
|
Full Usage:
Set.foldBack folder set state
Parameters:
'T -> 'State -> 'State
-
The accumulating function.
set : Set<'T>
-
The input set.
state : 'State
-
The initial state.
Returns: 'State
The final state.
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The sum of the set is {Set.foldBack (+) set 0}" printfn $"The set is {Set.foldBack (fun x acc -> x :: acc) set []}"The sample evaluates to the following output: The sum of the set is 6
The set is [1; 2; 3]
|
|
![]() ![]() ![]() ![]() ![]() ![]()
Tests if all elements of the collection satisfy the given predicate.
If the input function is
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"Does the set contain even numbers? {Set.forall (fun x -> x % 2 = 0) set}"The sample evaluates to the following output: Does the set contain even numbers? false
|
|
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(2).Add(3).Add(4) printfn $"The intersection of {set1} and {set2} is {Set.intersect set1 set2}"The sample evaluates to the following output: The intersection of set [1; 2; 3] and set [2; 3; 4] is set [2; 3]
|
|
Example
let headersByFile = seq{ yield [ "id"; "name"; "date"; "color" ] yield [ "id"; "age"; "date" ] yield [ "id"; "sex"; "date"; "animal" ] } headersByFile |> Seq.map Set.ofList |> Set.intersectMany |> printfn "The intersection of %A is %A" headersByFileThe sample evaluates to the following output: The intersection of seq
[["id"; "name"; "date"; "color"]; ["id"; "age"; "date"];
["id"; "sex"; "date"; "animal"]] is set ["date"; "id"]
|
|
Example
let set = Set.empty.Add(2).Add(3) printfn $"Is the set empty? {set.IsEmpty}"The sample evaluates to the following output: Is the set empty? false
|
|
![]() ![]() ![]() ![]() ![]() ![]() Evaluates to "true" if all elements of the first set are in the second, and at least one element of the second is not in the first.
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(1).Add(2).Add(3).Add(4) printfn $"Is {set1} a proper subset of {set2}? {Set.isProperSubset set1 set2}"The sample evaluates to the following output: Is set [1; 2; 3] a proper subset of set [1; 2; 3; 4]? true
|
|
![]() ![]() ![]() ![]() ![]() ![]() Evaluates to "true" if all elements of the second set are in the first, and at least one element of the first is not in the second.
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(1).Add(2).Add(3).Add(4) printfn $"Is {set1} a proper superset of {set2}? {Set.isProperSuperset set1 set2}"The sample evaluates to the following output: Is set [1; 2; 3] a proper superset of set [1; 2; 3; 4]? false
|
|
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(1).Add(2).Add(3).Add(4) printfn $"Is {set1} a subset of {set2}? {Set.isSubset set1 set2}"The sample evaluates to the following output: Is set [1; 2; 3] a subset of set [1; 2; 3; 4]? true
|
|
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(1).Add(2).Add(3).Add(4) printfn $"Is {set1} a superset of {set2}? {Set.isSuperset set1 set2}"The sample evaluates to the following output: Is set [1; 2; 3] a superset of set [1; 2; 3; 4]? false
|
|
![]() ![]() ![]() ![]() ![]() ![]() Applies the given function to each element of the set, in order according to the comparison function. Example
let set = Set.empty.Add(1).Add(2).Add(3) Set.iter (fun x -> printfn $"The set contains {x}") setThe sample evaluates to the following output:
The set contains 1
The set contains 2
The set contains 3
|
|
![]() ![]() ![]() ![]() ![]() ![]() Returns a new collection containing the results of applying the given function to each element of the input set.
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The set with doubled values is {Set.map (fun x -> x * 2) set}"The sample evaluates to the following output: The set with doubled values is set [2; 4; 6]
|
Full Usage:
Set.maxElement set
Parameters:
Set<'T>
-
The input set.
Returns: 'T
The max value from the set.
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The min element of {set} is {Set.minElement set}"The sample evaluates to the following output: The max element of set [1; 2; 3] is 3
|
Full Usage:
Set.minElement set
Parameters:
Set<'T>
-
The input set.
Returns: 'T
The min value from the set.
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The min element of {set} is {Set.minElement set}"The sample evaluates to the following output: The min element of set [1; 2; 3] is 1
|
Full Usage:
Set.ofArray array
Parameters:
'T[]
-
The input array.
Returns: Set<'T>
A set containing the elements of array .
|
Example
let set = Set.ofArray [|1, 2, 3|] printfn $"The set is {set} and type is {set.GetType().Name}"The sample evaluates to the following output: The set is set [(1, 2, 3)] and type is "FSharpSet`1"
|
|
Example
let set = Set.ofList [1, 2, 3] printfn $"The set is {set} and type is {set.GetType().Name}"The sample evaluates to the following output: The set is set [(1, 2, 3)] and type is "FSharpSet`1"
|
|
Example
let set = Set.ofSeq [1, 2, 3] printfn $"The set is {set} and type is {set.GetType().Name}"The sample evaluates to the following output: The set is set [(1, 2, 3)] and type is "FSharpSet`1"
|
![]() ![]() ![]() ![]() ![]() ![]() Splits the set into two sets containing the elements for which the given predicate returns true and false respectively.
Example
let set = Set.empty.Add(1).Add(2).Add(3).Add(4) printfn $"The set with even numbers is {Set.partition (fun x -> x % 2 = 0) set}"The sample evaluates to the following output: The partitioned sets are: (set [2; 4], set [1; 3])
|
|
|
![]() ![]() ![]() ![]() ![]() ![]() Returns a new set with the given element removed. No exception is raised if the set doesn't contain the given element.
Example
let set = Set.empty.Add(1).Add(2).Add(3) printfn $"The set without 1 is {Set.remove 1 set}"The sample evaluates to the following output: The set without 1 is set [2; 3]
|
Full Usage:
Set.singleton value
Parameters:
'T
-
The value for the set to contain.
Returns: Set<'T>
The set containing value .
|
Example
Set.singleton 7Evaluates to set [ 7 ] .
|
Full Usage:
Set.toArray set
Parameters:
Set<'T>
-
The input set.
Returns: 'T[]
An ordered array of the elements of set .
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) let array = Set.toArray set printfn$ "The set is {set} and type is {array.GetType().Name}"The sample evaluates to the following output: The set is [|1; 2; 3|] and type is System.Int32[]
|
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) let list = Set.toList set printfn $"The set is {list} and type is {list.GetType().Name}"The sample evaluates to the following output: The set is [1; 2; 3] and type is "FSharpList`1"
|
|
Example
let set = Set.empty.Add(1).Add(2).Add(3) let seq = Set.toSeq set printfn $"The set is {set} and type is {seq.GetType().Name}"The sample evaluates to the following output: he set is set [1; 2; 3] and type is Microsoft.FSharp.Collections.FSharpSet`1[System.Int32]
|
|
Example
let set1 = Set.empty.Add(1).Add(2).Add(3) let set2 = Set.empty.Add(2).Add(3).Add(4) printfn $"The union of {set1} and {set2} is {(Set.union set1 set2)}"The sample evaluates to the following output: The union of set [1; 2; 3] and set [2; 3; 4] is set [1; 2; 3; 4]
|
|
Example
let headersByFile = seq{ yield [ "id"; "name"; "date"; "color" ] yield [ "id"; "age"; "date" ] yield [ "id"; "sex"; "date"; "animal" ] } headersByFile |> Seq.map Set.ofList |> Set.intersectMany |> printfn "The intersection of %A is %A" headersByFileThe sample evaluates to the following output: The union of seq
[["id"; "name"; "date"; "color"]; ["id"; "age"; "date"];
["id"; "sex"; "date"; "animal"]] is set ["age"; "animal"; "color"; "date"; "id"; "name"; "sex"]
|