Version: 0.45.0

Set

Sets are unordered collections of unique values of the same type.

let empty: set<'elt>

Create an empty set.

let my_set: set<int> = Set.empty;
let literal: (input: list<'elt>) => set<'elt>

Create a non-empty set.

let my_set : set<int> = Set.literal(list([3, 2, 2, 1]));
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 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 add: (elt: 'elt, set: set<'elt>) => set<'elt>

Add a value to a set.

let updated_set = Set.add (4, my_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 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_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);
let iter: (iterator: ((item: 'a) => unit), set: set<'a>) => unit

Iterate over values in a set.

let iter_op = (s: set<int>): unit => {
let predicate = (i : int): unit => assert(i > 3);
Set.iter(predicate, s);
};
let fold: ((iterator: [acc: 'acc, item: 'item]) => 'acc), set: set<'item>, acc: 'acc) => 'acc

Fold over values in a set

let sum = ([acc, i]: [int, int]): int => acc + i;
let sum_of_elements: int = Set.fold(sum, my_set, 0);
let fold_desc: (((a: ['item, 'acc]) => 'acc), set<'item>, 'acc) => 'acc

Fold over values in a set

let sum_right = ([i, acc] : [int, int]) : int => acc + i;
let sum_of_elements_desc : int = Set.fold_desc (sum_right, my_set, 0);