ligo/gitlab-pages/docs/reference/set.md

5.6 KiB

id title description hide_table_of_contents
set-reference Set Set operations true

import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle';

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

function empty : set('value) val empty : 'value set let empty: set('value)

Create an empty set.

const my_set : set (int) = Set.empty

Alternative syntax:

const my_set : set (int) = set []
let my_set : int set = Set.empty
let my_set : set (int) = Set.empty;
function literal : list('value) -> set('value) val literal : 'value list -> 'value set let literal: list('value) => set('value)

Create a non-empty set.

const my_set : set (int) = Set.literal (list [3; 2; 2; 1])

Or use the following syntax sugar:

const my_set : set (int) = set [3; 2; 2; 1]
let my_set : int set =
  Set.literal [3; 2; 2; 1]
let my_set : set (int) =
  Set.literal ([3, 2, 2, 1]);
function mem : 'value -> set('value) -> 'bool val mem : 'value -> 'value set -> bool let mem: ('value, set('value)) => bool

Checks if a value exists in the set.

const contains_3 : bool = Set.mem(3, my_set)

Or:

const contains_3_alt : bool = my_set contains 3
let contains_3 : bool = Set.mem 3 my_set
let contains_3 : bool = Set.mem (3, my_set);
function cardinal : set('value) -> nat val cardinal : 'value set -> nat let cardinal: set('value) => nat

Number of elements in a set.

const cardinal : nat = Set.size (my_set)

Note that size is deprecated. Please use Set.size

let cardinal : nat = Set.size my_set
let cardinal : nat = Set.size (my_set);
function add : 'value -> set('value) -> set('value) val add : 'value -> 'value set -> 'value set let add: ('value, set('value)) => set('value)

Add a value to a set.

function remove : 'value -> set('value) -> set('value) val remove : 'value -> 'value set -> 'value set let remove: ('value, set('value)) => set('value)

Remove a value from a set.

function iter : ('a -> unit) -> set('a) -> unit val iter : ('a -> unit) -> 'a set -> unit let iter: (('a => unit), set('a)) => unit

Iterate over values in a set.

function iter_op (const s : set (int)) : unit is
  block {
    function iterated (const i : int) : unit is
      if i > 2 then Unit else (failwith ("Below range.") : unit)
  } with Set.iter (iterated, s)

Note that set_iter is deprecated.

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) => assert (i > 3);
  Set.iter (predicate, s);
};
function fold : (('accumulator -> 'item -> 'accumulator) -> set ('item) -> 'accumulator) -> 'accumulator val fold : ('accumulator -> 'item -> 'accumulator) -> 'set list -> 'accumulator -> 'accumulator let fold: ((('accumulator, 'item) => 'accumulator), set('item), 'accumulator) => 'accumulator

Fold over values in a set

function sum (const acc : int; const i : int): int is acc + i
const sum_of_elements : int = Set.fold (sum, my_set, 0)

Note that set_fold is deprecated.

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);