Header menu logo FSharp.Core

Set<'T> Type

Immutable sets based on binary trees, where elements are ordered by F# generic comparison. By default comparison is the F# structural comparison function or uses implementations of the IComparable interface on element values.

See the Set module for further operations on sets. All members of this class are thread-safe and may be used concurrently from multiple threads.

Constructors

Constructor Description

Set(elements)

Full Usage: Set(elements)

Parameters:
    elements : 'T seq - The input sequence.

Returns: Set<'T> The result set.

Create a set containing elements drawn from the given sequence.

elements : 'T seq

The input sequence.

Returns: Set<'T>

The result set.

Example

 let sequenceOfNumbers = seq { 1 .. 3 }
 let numbersInSet = Set(sequenceOfNumbers)
 printfn $"The set is {numbersInSet}"
val sequenceOfNumbers: int seq
Multiple items
val seq: sequence: 'T seq -> 'T seq

--------------------
type 'T seq = System.Collections.Generic.IEnumerable<'T>
val numbersInSet: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Instance members

Instance member Description

this.Add

Full Usage: this.Add

Parameters:
    value : 'T - The value to add to the set.

Returns: Set<'T> The result set.

A useful shortcut for Set.add. Note this operation produces a new set and does not mutate the original set. The new set will share many storage nodes with the original. See the Set module for further operations on sets.

value : 'T

The value to add to the set.

Returns: Set<'T>

The result set.

Example

 let set = Set.empty.Add(1).Add(1).Add(2)
 printfn $"The new set is: {set}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: The new set is: set [1; 2]

this.Contains

Full Usage: this.Contains

Parameters:
    value : 'T - The value to check.

Returns: bool True if the set contains value.

A useful shortcut for Set.contains. See the Set module for further operations on sets.

value : 'T

The value to check.

Returns: bool

True if the set contains value.

Example

 let set = Set.empty.Add(2).Add(3)
 printfn $"Does the set contain 1? {set.Contains(1)}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
member Set.Contains: value: 'T -> bool
The sample evaluates to the following output: Does the set contain 1? false

this.Count

Full Usage: this.Count

Returns: int

The number of elements in the set

Returns: int
Example

 let set = Set.empty.Add(1).Add(1).Add(2)
 printfn $"The set has {set.Count} elements"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property Set.Count: int with get
The sample evaluates to the following output: The set has 3 elements

this.IsEmpty

Full Usage: this.IsEmpty

Returns: bool

A useful shortcut for Set.isEmpty. See the Set module for further operations on sets.

Returns: bool
Example

 let set = Set.empty.Add(2).Add(3)
 printfn $"Is the set empty? {set.IsEmpty}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property Set.IsEmpty: bool with get
The sample evaluates to the following output: Is the set empty? false

this.IsProperSubsetOf

Full Usage: this.IsProperSubsetOf

Parameters:
    otherSet : Set<'T> - The set to test against.

Returns: bool True if this set is a proper subset of otherSet.

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.

otherSet : Set<'T>

The set to test against.

Returns: bool

True if this set is a proper subset of otherSet.

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}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: Is set [1; 2; 3] a proper subset of set [1; 2; 3; 4]? true

this.IsProperSupersetOf

Full Usage: this.IsProperSupersetOf

Parameters:
    otherSet : Set<'T> - The set to test against.

Returns: bool True if this set is a proper superset of otherSet.

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.

otherSet : Set<'T>

The set to test against.

Returns: bool

True if this set is a proper superset of otherSet.

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}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: Is set [1; 2; 3] a proper superset of set [1; 2; 3; 4]? false

this.IsSubsetOf

Full Usage: this.IsSubsetOf

Parameters:
    otherSet : Set<'T> - The set to test against.

Returns: bool True if this set is a subset of otherSet.

Evaluates to "true" if all elements of the first set are in the second.

otherSet : Set<'T>

The set to test against.

Returns: bool

True if this set is a subset of otherSet.

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}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: Is set [1; 2; 3] a subset of set [1; 2; 3; 4]? true

this.IsSupersetOf

Full Usage: this.IsSupersetOf

Parameters:
    otherSet : Set<'T> - The set to test against.

Returns: bool True if this set is a superset of otherSet.

Evaluates to "true" if all elements of the second set are in the first.

otherSet : Set<'T>

The set to test against.

Returns: bool

True if this set is a superset of otherSet.

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}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: Is set [1; 2; 3] a superset of set [1; 2; 3; 4]? false

this.MaximumElement

Full Usage: this.MaximumElement

Returns: 'T

Returns the highest element in the set according to the ordering being used for the set.

Returns: 'T
Example

 let set = Set.empty.Add(1).Add(2).Add(3)
 printfn $"MaximumElement: {set.MaximumElement}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property Set.MaximumElement: int with get
The sample evaluates to the following output: MaximumElement: 3

this.MinimumElement

Full Usage: this.MinimumElement

Returns: 'T

Returns the lowest element in the set according to the ordering being used for the set.

Returns: 'T
Example

 let set = Set.empty.Add(1).Add(2).Add(3)
 printfn $"MinimumElement: {set.MinimumElement}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property Set.MinimumElement: int with get
The sample evaluates to the following output: MinimumElement: 1

this.Remove

Full Usage: this.Remove

Parameters:
    value : 'T - The value to remove from the set.

Returns: Set<'T> The result set.

A useful shortcut for Set.remove. Note this operation produces a new set and does not mutate the original set. The new set will share many storage nodes with the original. See the Set module for further operations on sets.

value : 'T

The value to remove from the set.

Returns: Set<'T>

The result set.

Example

 let set = Set.empty.Add(1).Add(1).Add(2)
 printfn $"The new set is: {set}"
val set: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: The new set is: set [2]

Static members

Static member Description

set1 + set2

Full Usage: set1 + set2

Parameters:
    set1 : Set<'T> - The first input set.
    set2 : Set<'T> - The second input set.

Returns: Set<'T> The union of the two input sets.

Compute the union of the two sets.

set1 : Set<'T>

The first input set.

set2 : Set<'T>

The second input set.

Returns: Set<'T>

The union of the two input sets.

Example

 let set1 = Set.empty.Add(1).Add(2).Add(3)
 let set2 = Set.empty.Add(2).Add(3).Add(4)
 printfn $"Output is %A" {set1 + set2}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: The new set is: set [1; 2; 3; 4]

set1 - set2

Full Usage: set1 - set2

Parameters:
    set1 : Set<'T> - The first input set.
    set2 : Set<'T> - The second input set.

Returns: Set<'T> A set containing elements of the first set that are not contained in the second set.

Returns a new set with the elements of the second set removed from the first.

set1 : Set<'T>

The first input set.

set2 : Set<'T>

The second input set.

Returns: Set<'T>

A set containing elements of the first set that are not contained in the second set.

Example

 let set1 = Set.empty.Add(1).Add(2).Add(3)
 let set2 = Set.empty.Add(2).Add(3).Add(4)
 printfn $"The new set is: {set1 - set2}"
val set1: Set<int>
Multiple items
module Set from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> = interface IReadOnlyCollection<'T> interface IStructuralEquatable interface IComparable interface IEnumerable interface IEnumerable<'T> interface ICollection<'T> new: elements: 'T seq -> Set<'T> member Add: value: 'T -> Set<'T> member Contains: value: 'T -> bool override Equals: obj -> bool ...

--------------------
new: elements: 'T seq -> Set<'T>
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
val set2: Set<int>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
The sample evaluates to the following output: The new set is: set [1]

Type something to start searching.