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

183 lines
4.1 KiB
Markdown
Raw Normal View History

---
id: list-reference
2020-03-17 19:38:41 +04:00
title: List
description: List operations
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
2020-03-17 19:38:41 +04:00
import SyntaxTitle from '@theme/SyntaxTitle';
<SyntaxTitle syntax="pascaligo">
function length : nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val length : nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let length: nat
</SyntaxTitle>
2020-03-17 19:38:41 +04:00
Get the number of elements in a list.
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function size : nat
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val size : nat
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let size: nat
</SyntaxTitle>
2020-03-17 19:38:41 +04:00
Get the number of elements in a list.
2020-03-17 19:38:41 +04:00
Synonym for `List.length`.
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function iter : ('a -> unit) -> list('a) -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val iter : ('a -> unit) -> 'a list -> unit
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let iter: (('a => unit), list('a)) => unit
</SyntaxTitle>
2020-03-17 19:38:41 +04:00
Iterate over items in a list.
<Syntax syntax="pascaligo">
```pascaligo group=lists
function iter_op (const l : list (int)) : unit is
block {
function iterated (const i : int) : unit is
if i > 3 then Unit else (failwith ("Below range.") : unit)
} with List.iter (iterated, l)
```
> Note that `list_iter` is *deprecated*.
2020-03-17 19:38:41 +04:00
Alternatively it's also possible to use [loops](../language-basics/loops.md).
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=lists
let iter_op (l : int list) : unit =
let predicate = fun (i : int) -> assert (i > 3)
in List.iter predicate l
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=lists
let iter_op = (l : list (int)) : unit => {
let predicate = (i : int) => assert (i > 3);
List.iter (predicate, l);
};
```
</Syntax>
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function map : ('a -> 'b) -> list('a) -> list('b)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val map : ('a -> 'b) -> 'a list -> 'b list
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let map: (('a => 'b), list('a)) => list('b)
</SyntaxTitle>
2020-03-17 19:38:41 +04:00
Apply a function to items of a list to create a new list.
<Syntax syntax="pascaligo">
```pascaligo group=lists
2020-03-17 19:38:41 +04:00
const larger_list: list(int) = list [1; 2; 3]
function increment (const i : int): int is i + 1
// Creates a new list with all elements incremented by 1
const plus_one : list (int) = List.map (increment, larger_list)
```
> Note that `list_map` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=lists
2020-03-17 19:38:41 +04:00
let larger_list: int list = [1; 2; 3]
let increment (i : int) : int = i + 1
// Creates a new list with all elements incremented by 1
let plus_one : int list = List.map increment larger_list
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=lists
2020-03-17 19:38:41 +04:00
let larger_list: list(int) = [1, 2, 3];
let increment = (i : int) : int => i + 1;
// Creates a new list with all elements incremented by 1
let plus_one : list (int) = List.map (increment, larger_list);
```
</Syntax>
2020-03-17 19:38:41 +04:00
<SyntaxTitle syntax="pascaligo">
function fold : (('accumulator -> 'item -> 'accumulator) -> list('item) -> 'accumulator) -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val fold : ('accumulator -> 'item -> 'accumulator) -> 'item list -> 'accumulator -> 'accumulator
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let fold: ((('accumulator, 'item) => 'accumulator), list('item), 'accumulator) => 'accumulator
</SyntaxTitle>
2020-03-17 19:38:41 +04:00
[Fold over items in a list](../language-basics/sets-lists-tuples#folded-operation-over-lists);
<Syntax syntax="pascaligo">
```pascaligo group=lists
2020-03-17 19:38:41 +04:00
const my_list: list(int) = list [1; 2; 3]
function sum (const acc : int; const i : int): int is acc + i
2020-03-17 19:38:41 +04:00
const sum_of_elements : int = List.fold (sum, my_list, 0)
```
> Note that `list_fold` is *deprecated*.
</Syntax>
<Syntax syntax="cameligo">
```cameligo group=lists
2020-03-17 19:38:41 +04:00
let my_list : int list = [1; 2; 3]
let sum (acc, i : int * int) : int = acc + i
let sum_of_elements : int = List.fold sum my_list 0
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo group=lists
2020-03-17 19:38:41 +04:00
let my_list : list(int) = [1, 2, 3];
2020-03-17 19:38:41 +04:00
let sum = ((result, i): (int, int)): int => result + i;
2020-03-17 19:38:41 +04:00
let sum_of_elements : int = List.fold (sum, my_list, 0);
```
</Syntax>