Skip to main content

Set

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

cameligo

val empty : 'elt set

jsligo

let empty: set<'elt>

Create an empty set.

cameligo
let my_set : int set = Set.empty
jsligo
let my_set: set<int> = Set.empty;
cameligo

val literal : 'elt list -> 'elt set

jsligo

let literal: (input: list<'elt>) => set<'elt>

Create a non-empty set.

cameligo
let my_set : int set = Set.literal [3; 2; 2; 1]
jsligo
let my_set : set<int> = Set.literal(list([3, 2, 2, 1]));
cameligo

val mem : 'elt -> 'elt set -> bool

jsligo

let mem: (v: 'elt, set: set<'elt>) => bool

Checks if a value exists in the set.

cameligo
let contains_3 : bool = Set.mem 3 my_set
jsligo
let contains_3 : bool = Set.mem (3, my_set);
cameligo

val cardinal : 'elt set -> nat

jsligo

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.

cameligo
let card : nat = Set.cardinal my_set
jsligo
let card: nat = Set.cardinal(my_set);
cameligo

val add : 'elt -> 'elt set -> 'elt set

jsligo

let add: (elt: 'elt, set: set<'elt>) => set<'elt>

Add a value to a set.

cameligo
let updated_set = Set.add 4 my_set
jsligo
let updated_set = Set.add (4, my_set);
cameligo

val remove : 'elt -> 'elt set -> 'elt set

jsligo

let remove: (elt: 'elt, set: set<'elt>) => set<'elt>

Remove a value from a set.

cameligo
let updated_set = Set.remove 3 my_set
jsligo
let updated_set = Set.remove (3, my_set);
cameligo

val update : 'elt -> bool -> 'elt set -> 'elt set

jsligo

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.

cameligo
(* 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
jsligo
// 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);
cameligo

val iter : ('elt -> unit) -> 'elt set -> unit

jsligo

let iter: (iterator: ((item: 'a) => unit), set: set<'a>) => unit

Iterate over values in a set.

cameligo
let iter_op (s : int set) : unit =
let predicate = fun (i : int) -> assert (i > 3)
in Set.iter predicate s
jsligo
let iter_op = (s: set<int>): unit => {
let predicate = (i : int): unit => assert(i > 3);
Set.iter(predicate, s);
};
cameligo

val fold : ('acc * 'item -> 'acc) -> 'item set -> 'acc -> 'acc

jsligo

let fold: ((iterator: [acc: 'acc, item: 'item]) => 'acc), set: set<'item>, acc: 'acc) => 'acc

Fold over values in a set

cameligo
let sum (acc, i : int * int) : int = acc + i
let sum_of_elements : int = Set.fold sum my_set 0
jsligo
let sum = ([acc, i]: [int, int]): int => acc + i;
let sum_of_elements: int = Set.fold(sum, my_set, 0);
cameligo

val fold_desc : (('item * 'acc) -> 'acc) -> 'item set -> 'acc -> 'acc

jsligo

let fold_desc: (((a: ['item, 'acc]) => 'acc), set<'item>, 'acc) => 'acc

Fold over values in a set

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

val filter_map : ('a -> 'b option) -> 'a set -> 'b set

jsligo

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.