Add set reference page to the docs

Add Set.literal to the test suite
This commit is contained in:
John David Pressman 2020-02-12 01:39:41 +00:00
parent 03c2fe28a0
commit e8da2626e4
5 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,201 @@
---
id: set-reference
title: Set
---
## Defining a set
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
type int_set is set (int);
const my_set : int_set = set 1; 2; 3 end
```
<!--CameLIGO-->
```cameligo group=a
type int_set = int set
let my_set : int_set =
Set.add 3 (Set.add 2 (Set.add 1 (Set.empty: int set)))
```
<!--ReasonLIGO-->
```reasonligo group=a
type int_set = set (int);
let my_set : int_set =
Set.add (3, Set.add (2, Set.add (1, Set.empty: set (int))));
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.mem(is_member: a', s: a' set) : bool
Check if a set `s` contains the element `is_member`.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
const contains_three : bool = my_set contains 3
// or alternatively
const contains_three_fn: bool = set_mem (3, my_set);
```
<!--CameLIGO-->
```cameligo group=a
let contains_three: bool = Set.mem 3 my_set
```
<!--ReasonLIGO-->
```reasonligo group=a
let contains_three: bool = Set.mem(3, my_set);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.empty() : a' set
Create a new empty set. Needs to be annotated with the set type.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
const my_set: int_set = set end
const my_set_2: int_set = set_empty
```
<!--CameLIGO-->
```cameligo group=a
let my_set: int_set = (Set.empty: int set)
```
<!--ReasonLIGO-->
```reasonligo group=a
let my_set: int_set = (Set.empty: set (int));
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.literal(element_list_literal: 'a list) : 'a set
Create a set from the elements of a list. Note that **you must pass a list literal**
to this function, a variable will not work.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
const s_fb : set(string) = set [
"foo" ;
"bar" ;
]
```
<!--CameLIGO-->
```cameligo
let literal_op (p: unit) : string set =
Set.literal ["foo"; "bar"; "foobar"]
```
<!--ReasonLIGO-->
```reasonligo
let literal_op = (p: unit) : set(string) => Set.literal(["foo", "bar", "foobar"]);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.add(addition: a', s: a' set) : a' set
Add the element `addition` to a set `s`.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
function add_op (const s : set(string)) : set(string) is
begin skip end with set_add("foobar" , s)
```
<!--CameLIGO-->
```cameligo group=a
type int_set = int set
let my_set : int_set =
Set.add 3 (Set.add 2 (Set.add 1 (Set.empty: int set)))
```
<!--ReasonLIGO-->
```reasonligo group=a
type int_set = set (int);
let my_set : int_set =
Set.add (3, Set.add (2, Set.add (1, Set.empty: set (int))));
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.remove(removal: a', s: a' set) : a' set
Remove the element `removal` from a set `s`.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
const smaller_set: int_set = set_remove(3, my_set);
```
<!--CameLIGO-->
```cameligo group=a
let smaller_set: int_set = Set.remove 3 my_set
```
<!--ReasonLIGO-->
```reasonligo group=a
let smaller_set: int_set = Set.remove(3, my_set);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.fold(folding_function: a' -> a' -> a', s: a' set, initial: a') : a'
Combine the elements of a set into a single value using a folding function.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
function sum(const result: int; const i: int): int is result + i;
// Outputs 6
const sum_of_a_set: int = set_fold(sum, my_set, 0);
```
<!--CameLIGO-->
```cameligo group=a
let sum (result, i: int * int) : int = result + i
let sum_of_a_set: int = Set.fold sum my_set 0
```
<!--ReasonLIGO-->
```reasonligo group=a
let sum = (result_i: (int, int)): int => result_i[0] + result_i[1];
let sum_of_a_set: int = Set.fold(sum, my_set, 0);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Set.size(s: a' set) : nat
Get the number of elements in a set.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```pascaligo group=a
const set_size: nat = size (my_set)
```
<!--CameLIGO-->
```cameligo group=a
let set_size: nat = Set.size my_set
```
<!--ReasonLIGO-->
```reasonligo group=a
let set_size: nat = Set.size (my_set);
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -1,5 +1,8 @@
(* Test set operations in CameLIGO *)
let literal_op (p: unit) : string set =
Set.literal ["foo"; "bar"; "foobar"]
let add_op (s : string set) : string set =
Set.add "foobar" s

View File

@ -1,5 +1,7 @@
/* Test set operations in ReasonLIGO */
let literal_op = (p: unit) : set(string) => Set.literal(["foo", "bar", "foobar"]);
let add_op = (s: set(string)): set(string) => Set.add("foobar", s);
let remove_op = (s: set(string)): set(string) => Set.remove("foobar", s);

View File

@ -582,6 +582,11 @@ let set_arithmetic () : unit result =
let set_arithmetic_mligo () : unit result =
let%bind program = mtype_file "./contracts/set_arithmetic.mligo" in
let%bind program_1 = type_file "./contracts/set_arithmetic-1.ligo" in
let%bind () =
expect_eq program "literal_op"
(e_unit ())
(e_set [e_string "foo"; e_string "bar"; e_string "foobar"])
in
let%bind () =
expect_eq program "size_op"
(e_set [e_string "foo"; e_string "bar"; e_string "foobar"])
@ -612,6 +617,11 @@ let set_arithmetic_mligo () : unit result =
let set_arithmetic_religo () : unit result =
let%bind program = retype_file "./contracts/set_arithmetic.religo" in
let%bind program_1 = type_file "./contracts/set_arithmetic-1.ligo" in
let%bind () =
expect_eq program "literal_op"
(e_unit ())
(e_set [e_string "foo"; e_string "bar"; e_string "foobar"])
in
let%bind () =
expect_eq program "size_op"
(e_set [e_string "foo"; e_string "bar"; e_string "foobar"])

View File

@ -122,6 +122,7 @@ let md_files = [
"/gitlab-pages/docs/advanced/timestamps-addresses.md";
"/gitlab-pages/docs/api/cli-commands.md";
"/gitlab-pages/docs/api/cheat-sheet.md";
"/gitlab-pages/docs/reference/set.md";
"/gitlab-pages/docs/reference/big_map.md";
"/gitlab-pages/docs/reference/string.md";
]