Set
Sets are unordered collections of unique values of the same type.
val empty : 'elt set
let empty: set<'elt>
Create an empty set.
let my_set : int set = Set.empty
let my_set: set<int> = Set.empty;
val literal : 'elt list -> 'elt set
let literal: (input: list<'elt>) => set<'elt>
Create a non-empty set.
let my_set : int set = Set.literal [3; 2; 2; 1]
let my_set : set<int> = Set.literal(list([3, 2, 2, 1]));
val mem : 'elt -> 'elt set -> bool
let mem: (v: 'elt, set: set<'elt>) => bool
Checks if a value exists in the set.
let contains_3 : bool = Set.mem 3 my_set
let contains_3 : bool = Set.mem (3, my_set);
val cardinal : 'elt set -> nat
let cardinal: (set: set<'elt>) => nat
Number of elements in a set.
Notice: Previously, Set.size
was used for the number of elements in
a set. Set.size
is now marked for deprecation, and Set.cardinal
should be used instead.
let card : nat = Set.cardinal my_set
let card: nat = Set.cardinal(my_set);
val add : 'elt -> 'elt set -> 'elt set
let add: (elt: 'elt, set: set<'elt>) => set<'elt>
Add a value to a set.
let updated_set = Set.add 4 my_set
let updated_set = Set.add (4, my_set);
val remove : 'elt -> 'elt set -> 'elt set
let remove: (elt: 'elt, set: set<'elt>) => set<'elt>
Remove a value from a set.
let updated_set = Set.remove 3 my_set
let updated_set = Set.remove (3, my_set);
val update : 'elt -> bool -> 'elt set -> 'elt set
let update: (elt : 'elt, flag : bool, set : set<'elt>) => set<'elt>
Add or remove an element in a set based on the boolean value being passed.
(* in case of true value will be added to the set *)
let updated_set = Set.update 4 true my_set
(* in case of false value will be removed from the set *)
let updated_set = Set.update 4 false my_set
// in case of true value will be added to the set
let updated_set2 = Set.update (4, true, my_set);
// in case of false value will be removed from the set
let updated_set3 = Set.update (4, false, my_set);
val iter : ('elt -> unit) -> 'elt set -> unit
let iter: (iterator: ((item: 'a) => unit), set: set<'a>) => unit
Iterate over values in a set.
let iter_op (s : int set) : unit =
let predicate = fun (i : int) -> assert (i > 3)
in Set.iter predicate s
let iter_op = (s: set<int>): unit => {
let predicate = (i : int): unit => assert(i > 3);
Set.iter(predicate, s);
};
val fold : ('acc * 'item -> 'acc) -> 'item set -> 'acc -> 'acc
let fold: ((iterator: [acc: 'acc, item: 'item]) => 'acc), set: set<'item>, acc: 'acc) => 'acc
let sum (acc, i : int * int) : int = acc + i
let sum_of_elements : int = Set.fold sum my_set 0
let sum = ([acc, i]: [int, int]): int => acc + i;
let sum_of_elements: int = Set.fold(sum, my_set, 0);
val fold_desc : (('item * 'acc) -> 'acc) -> 'item set -> 'acc -> 'acc
let fold_desc: (((a: ['item, 'acc]) => 'acc), set<'item>, 'acc) => 'acc
let sum_right (i, acc : int * int) : int = acc + i
let sum_of_elements : int = Set.fold_desc sum_right my_set 0
let sum_right = ([i, acc]: [int, int]) : int => acc + i;
let sum_of_elements_desc : int = Set.fold_desc (sum_right, my_set, 0);
val filter_map : ('a -> 'b option) -> 'a set -> 'b set
let filter_map: (mapper: ((item: 'a) => option<'b>), set: set<'a>) => set<'b>
Apply a function to items of a set to create a new set, but the function can omit certain elements by returning None
.
Notice: built in terms of fold_desc
.