2020-02-12 05:39:41 +04:00
|
|
|
---
|
|
|
|
id: set-reference
|
2020-03-17 19:38:41 +04:00
|
|
|
title: Set
|
|
|
|
description: Set operations
|
|
|
|
hide_table_of_contents: true
|
2020-02-12 05:39:41 +04:00
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
2020-03-17 19:38:41 +04:00
|
|
|
import SyntaxTitle from '@theme/SyntaxTitle';
|
|
|
|
|
|
|
|
Sets are unordered collections of unique values of the same type.
|
|
|
|
|
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
type set ('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
type 'value set
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
type set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
|
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function empty : set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val empty : 'value set
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let empty: set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
|
|
|
|
Create an empty set.
|
2020-03-04 17:19:00 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<Syntax syntax="pascaligo">
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
const my_set : set (int) = Set.empty
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
Alternative syntax:
|
2020-03-04 17:19:00 +04:00
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
const my_set : set (int) = set []
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let my_set : int set = Set.empty
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let my_set : set (int) = Set.empty;
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function literal : list('value) -> set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val literal : 'value list -> 'value set
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let literal: list('value) => set('value)
|
|
|
|
</SyntaxTitle>
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
Create a non-empty set.
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
const my_set : set (int) = Set.literal (list [3; 2; 2; 1])
|
|
|
|
```
|
|
|
|
|
|
|
|
Or use the following syntax sugar:
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
const my_set : set (int) = set [3; 2; 2; 1]
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let my_set : int set =
|
2020-03-17 19:38:41 +04:00
|
|
|
Set.literal [3; 2; 2; 1]
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-02-26 21:31:58 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let my_set : set (int) =
|
2020-03-17 19:38:41 +04:00
|
|
|
Set.literal ([3, 2, 2, 1]);
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function mem : 'value -> set('value) -> 'bool
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val mem : 'value -> 'value set -> bool
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let mem: ('value, set('value)) => bool
|
|
|
|
</SyntaxTitle>
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
Checks if a value exists in the set.
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
2020-03-17 19:38:41 +04:00
|
|
|
const contains_3 : bool = Set.mem(3, my_set)
|
|
|
|
```
|
|
|
|
|
|
|
|
Or:
|
|
|
|
|
|
|
|
```pascaligo group=sets
|
|
|
|
const contains_3_alt : bool = my_set contains 3
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-02-26 21:31:58 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let contains_3 : bool = Set.mem 3 my_set
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-02-26 21:31:58 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let contains_3 : bool = Set.mem (3, my_set);
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function cardinal : set('value) -> nat
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val cardinal : 'value set -> nat
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let cardinal: set('value) => nat
|
|
|
|
</SyntaxTitle>
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
Number of elements in a set.
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
const cardinal : nat = Set.size (my_set)
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
> Note that `size` is *deprecated*. Please use `Set.size`
|
2020-02-26 21:31:58 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let cardinal : nat = Set.size my_set
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let cardinal : nat = Set.size (my_set);
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function add : 'value -> set('value) -> set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val add : 'value -> 'value set -> 'value set
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let add: ('value, set('value)) => set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
|
|
|
|
Add a value to a set.
|
|
|
|
|
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function remove : 'value -> set('value) -> set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val remove : 'value -> 'value set -> 'value set
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let remove: ('value, set('value)) => set('value)
|
|
|
|
</SyntaxTitle>
|
|
|
|
|
|
|
|
Remove a value from a set.
|
|
|
|
|
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function iter : ('a -> unit) -> set('a) -> unit
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val iter : ('a -> unit) -> 'a set -> unit
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let iter: (('a => unit), set('a)) => unit
|
|
|
|
</SyntaxTitle>
|
|
|
|
|
|
|
|
Iterate over values in a set.
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
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)
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
> Note that `set_iter` is *deprecated*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let iter_op (s : int set) : unit =
|
|
|
|
let predicate = fun (i : int) -> assert (i > 3)
|
|
|
|
in Set.iter predicate s
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let iter_op = (s : set (int)) : unit => {
|
|
|
|
let predicate = (i : int) => assert (i > 3);
|
|
|
|
Set.iter (predicate, s);
|
|
|
|
};
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
<SyntaxTitle syntax="pascaligo">
|
|
|
|
function fold : (('accumulator -> 'item -> 'accumulator) -> set ('item) -> 'accumulator) -> 'accumulator
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="cameligo">
|
|
|
|
val fold : ('accumulator -> 'item -> 'accumulator) -> 'set list -> 'accumulator -> 'accumulator
|
|
|
|
</SyntaxTitle>
|
|
|
|
<SyntaxTitle syntax="reasonligo">
|
|
|
|
let fold: ((('accumulator, 'item) => 'accumulator), set('item), 'accumulator) => 'accumulator
|
|
|
|
</SyntaxTitle>
|
2020-02-12 05:39:41 +04:00
|
|
|
|
2020-03-17 19:38:41 +04:00
|
|
|
[Fold over values in a set](../language-basics/sets-lists-tuples#folded-operation)
|
2020-02-26 21:31:58 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```pascaligo group=sets
|
|
|
|
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*.
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```cameligo group=sets
|
|
|
|
let sum (acc, i : int * int) : int = acc + i
|
|
|
|
let sum_of_elements : int = Set.fold sum my_set 0
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-26 21:31:58 +04:00
|
|
|
```reasonligo group=sets
|
|
|
|
let sum = ((acc, i) : (int, int)) : int => acc + i;
|
|
|
|
let sum_of_elements : int = Set.fold (sum, my_set, 0);
|
2020-02-12 05:39:41 +04:00
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|