Fsharp-sets
提供:Dev Guides
F#-セット
F#のセットは、アイテムが挿入される順序を保持せずにアイテムのコレクションとして機能するデータ構造です。 セットでは、コレクションに重複エントリを挿入できません。
セットを作成する
セットは、次の方法で作成することができます-
- Set.emptyを使用して空のセットを作成し、add関数を使用してアイテムを追加します。
- シーケンスとリストをセットに変換します。
次のプログラムは、テクニックを示しています-
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-
セットの基本操作
次の表は、セットの基本的な操作を示しています-
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. |
次の例は、上記の機能のいくつかの使用を示しています-
例
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-