F Sharp Sets - F Sharp (programming language)

What is F# Sets?

A set in F# is a data structure that acts as a collection of items without preserving the order in which items are inserted. Sets do not allow duplicate entries to be inserted into the collection.

Creating Sets

Sets can be created in the following ways −

  • By creating an empty set using Set.empty and adding items using the add function.
  • Converting sequences and lists to sets.

The following program demonstrates the techniques −

On compiling and running, we get the below output

Basic Operations on Sets

The following table shows the basic operations on sets −

Value

Description

add : 'T → Set<'T> → Set<'T>

Returns a new set with an element added to the set. No exception is raised if the set already contains the given element.

contains : 'T → Set<'T> → bool

Evaluates to true if the given element is in the given set.

count : Set<'T> → int

Returns the number of elements in the set.

difference : Set<'T> → Set<'T> → Set<'T>

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

empty : Set<'T>

The empty set for the specified type.

exists : ('T → bool) → Set<'T> → bool

Tests if any element of the collection satisfies the given predicate. If the input function is predicate and the elements are i0...iN, then this function computes predicate i0 or ... or predicate iN.

filter : ('T → bool) → Set<'T> → Set<'T>

Returns a new collection containing only the elements of the collection for which the given predicate returns true.

fold : ('State → 'T → 'State) → 'State → Set<'T> → 'State

Applies the given accumulating function to all the elements of the set.

foldBack : ('T → 'State → 'State) → Set<'T> → 'State → 'State

Applies the given accumulating function to all the elements of the set.

forall : ('T → bool) → Set<'T> → bool

Tests if all elements of the collection satisfy the given predicate. If the input function is p and the elements are i0...iN, then this function computes p i0 && ... && p iN.

intersect : Set<'T> → Set<'T> → Set<'T>

Computes the intersection of the two sets.

intersectMany : seq<Set<'T>> → Set<'T>

Computes the intersection of a sequence of sets. The sequence must be non-empty.

isEmpty : Set<'T> → bool

Returns true if the set is empty.

isProperSubset : Set<'T> → Set<'T> → bool

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.

isProperSuperset : Set<'T> → Set<'T> → bool

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.

isSubset : Set<'T> → Set<'T> → bool

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

isSuperset : Set<'T> → Set<'T> → bool

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

iter : ('T → unit) → Set<'T> → unit

Applies the given function to each element of the set, in order according to the comparison function.

map : ('T → 'U) → Set<'T> → Set<'U>

Returns a new collection containing the results of applying the given function to each element of the input set.

maxElement : Set<'T> → 'T

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

minElement : Set<'T> → 'T

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

ofArray : 'T array → Set<'T>

Creates a set that contains the same elements as the given array.

ofList : 'T list → Set<'T>

Creates a set that contains the same elements as the given list.

ofSeq : seq<'T> → Set<'T>

Creates a new collection from the given enumerable object.

partition : ('T → bool) → Set<'T> → Set<'T> * Set<'T>

Splits the set into two sets containing the elements for which the given predicate returns true and false respectively.

remove : 'T → Set<'T> → Set<'T>

Returns a new set with the given element removed. No exception is raised if the set doesn't contain the given element.

singleton : 'T → Set<'T>

The set containing the given element.

toArray : Set<'T> → 'T array

Creates an array that contains the elements of the set in order.

toList : Set<'T> → 'T list

Creates a list that contains the elements of the set in order.

toSeq : Set<'T> → seq<'T>

Returns an ordered view of the collection as an enumerable object.

union : Set<'T> → Set<'T> → Set<'T>

Computes the union of the two sets.

unionMany : seq<Set<'T>> → Set<'T>

Computes the union of a sequence of sets.

The following example demonstrates the uses of some of the above functionalities −

Example

On compiling and running, we get the below output

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

F Sharp (programming language) Topics