diff --git a/gitlab-pages/docs/reference/set.md b/gitlab-pages/docs/reference/set.md new file mode 100644 index 000000000..a1de140b6 --- /dev/null +++ b/gitlab-pages/docs/reference/set.md @@ -0,0 +1,201 @@ +--- +id: set-reference +title: Set +--- + +## Defining a set + + + +```pascaligo group=a +type int_set is set (int); +const my_set : int_set = set 1; 2; 3 end +``` + + +```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 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)))); +``` + + + + +## Set.mem(is_member: a', s: a' set) : bool + +Check if a set `s` contains the element `is_member`. + + + +```pascaligo group=a +const contains_three : bool = my_set contains 3 +// or alternatively +const contains_three_fn: bool = set_mem (3, my_set); +``` + + +```cameligo group=a +let contains_three: bool = Set.mem 3 my_set +``` + +```reasonligo group=a +let contains_three: bool = Set.mem(3, my_set); +``` + + + + +## Set.empty() : a' set + +Create a new empty set. Needs to be annotated with the set type. + + + +```pascaligo group=a +const my_set: int_set = set end +const my_set_2: int_set = set_empty +``` + +```cameligo group=a +let my_set: int_set = (Set.empty: int set) +``` + +```reasonligo group=a +let my_set: int_set = (Set.empty: set (int)); +``` + + +## 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. + + + + +```pascaligo +const s_fb : set(string) = set [ + "foo" ; + "bar" ; +] +``` + + +```cameligo +let literal_op (p: unit) : string set = + Set.literal ["foo"; "bar"; "foobar"] +``` + + +```reasonligo +let literal_op = (p: unit) : set(string) => Set.literal(["foo", "bar", "foobar"]); +``` + + + +## Set.add(addition: a', s: a' set) : a' set + +Add the element `addition` to a set `s`. + + + +```pascaligo group=a +function add_op (const s : set(string)) : set(string) is + begin skip end with set_add("foobar" , s) +``` + + +```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 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)))); +``` + + + +## Set.remove(removal: a', s: a' set) : a' set + +Remove the element `removal` from a set `s`. + + + +```pascaligo group=a +const smaller_set: int_set = set_remove(3, my_set); +``` + + + +```cameligo group=a +let smaller_set: int_set = Set.remove 3 my_set +``` + + + +```reasonligo group=a +let smaller_set: int_set = Set.remove(3, my_set); +``` + + + + +## 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. + + + +```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 group=a +let sum (result, i: int * int) : int = result + i +let sum_of_a_set: int = Set.fold sum my_set 0 +``` + + +```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); +``` + + +## Set.size(s: a' set) : nat + +Get the number of elements in a set. + + + +```pascaligo group=a +const set_size: nat = size (my_set) +``` + + +```cameligo group=a +let set_size: nat = Set.size my_set +``` + + +```reasonligo group=a +let set_size: nat = Set.size (my_set); +``` + + diff --git a/src/test/contracts/set_arithmetic.mligo b/src/test/contracts/set_arithmetic.mligo index 74fdc170f..2713905b3 100644 --- a/src/test/contracts/set_arithmetic.mligo +++ b/src/test/contracts/set_arithmetic.mligo @@ -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 diff --git a/src/test/contracts/set_arithmetic.religo b/src/test/contracts/set_arithmetic.religo index 4e613500d..5aa634333 100644 --- a/src/test/contracts/set_arithmetic.religo +++ b/src/test/contracts/set_arithmetic.religo @@ -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); diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index f384db470..415bb354b 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -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"]) diff --git a/src/test/md_file_tests.ml b/src/test/md_file_tests.ml index 247f396f1..908f6ccd0 100644 --- a/src/test/md_file_tests.ml +++ b/src/test/md_file_tests.ml @@ -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"; ]