628d818163
This reverts commit 6ffe220d928dc3137496bcea0cc0f4d72edc2846.
4.9 KiB
4.9 KiB
id | title |
---|---|
sets-lists-touples | Sets, Lists, Tuples |
Apart from complex data types such as maps
and records
, ligo also exposes sets
, lists
and tuples
.
⚠️ Make sure to pick the appropriate data type for your use case; it carries not only semantic but also gas related costs.
Sets
Sets are similar to lists. The main difference is that elements of a set
must be unique.
Defining a set
type int_set is set(int);
const my_set: int_set = set
1;
2;
3;
end
type int_set = int set
let my_set: int_set =
Set.add 3 (Set.add 2 (Set.add 1 Set.empty))
Empty sets
const my_set: int_set = set end;
const my_set_2: int_set = set_empty;
Checking if set contains an element
const contains_three: bool = my_set contains 3;
// or alternatively
const contains_three_fn: bool = set_mem(3, my_set);
let contains_three: bool = Set.mem 3 my_set
Obtaining the size of a set
const set_size: nat = size(my_set);
let set_size: nat = Set.size my_set
Modifying a set
const larger_set: int_set = set_add(4, my_set);
const smaller_set: int_set = set_remove(3, my_set);
let larger_set: int_set = Set.add 4 my_set
let smaller_set: int_set = Set.remove 3 my_set
Folding a set
function sum(const result: int; const i: int): int is result + i;
// Outputs 6
const sum_of_a_set: int = set_fold(my_set, 0, sum);
let sum (result: int) (i: int) : int = result + i
let sum_of_a_set: int = Set.fold my_set 0 sum
Lists
Lists are similar to sets, but their elements don't need to be unique and they don't offer the same range of built-in functions.
💡 Lists are useful when returning operations from a smart contract's entrypoint.
Defining a list
type int_list is list(int);
const my_list: int_list = list
1;
2;
3;
end
type int_list = int list
let my_list: int_list = [1; 2; 3]
Appending an element to a list
const larger_list: int_list = cons(4, my_list);
const even_larger_list: int_list = 5 # larger_list;
let larger_list: int_list = 4 :: my_list
(* CameLIGO doesn't have a List.cons *)
> 💡 Lists can be iterated, folded or mapped to different values. You can find additional examples [here](https://gitlab.com/ligolang/ligo/tree/dev/src/test/contracts) and other built-in operators [here](https://gitlab.com/ligolang/ligo/blob/dev/src/passes/operators/operators.ml#L59)
Mapping of a list
function increment(const i: int): int is block { skip } with i + 1;
// Creates a new list with elements incremented by 1
const incremented_list: int_list = list_map(even_larger_list, increment);
let increment (i: int) : int = i + 1
(* Creates a new list with elements incremented by 1 *)
let incremented_list: int_list = List.map larger_list increment
Folding of a list:
function sum(const result: int; const i: int): int is block { skip } with result + i;
// Outputs 6
const sum_of_a_list: int = list_fold(my_list, 0, sum);
let sum (result: int) (i: int) : int = result + i
// Outputs 6
let sum_of_a_list: int = List.fold my_list 0 sum
Tuples
Tuples are useful for data that belong together but don't have an index or a specific name.
Defining a tuple
type full_name is string * string;
const full_name: full_name = ("Alice", "Johnson");
type full_name = string * string
(* The parenthesis here are optional *)
let full_name: full_name = ("Alice", "Johnson")
Accessing an element in a tuple
const first_name: string = full_name.1;
let first_name: string = full_name.1