package octez-libs
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=c6df840ebbf115e454db949028c595bec558a59a66cade73b52a6d099d6fa4d4
sha512=d8aee903b9fe130d73176bc8ec38b78c9ff65317da3cb4f3415f09af0c625b4384e7498201fdb61aa39086a7d5d409d0ab3423f9bc3ab989a680cf444a79bc13
doc/octez-libs.stdlib/Tezos_stdlib/FunctionalArray/index.html
Module Tezos_stdlib.FunctionalArray
Source
This module implements functional arrays equipped with accessors that cannot raise exceptions following the same design principles as FallbackArray
:
Reading out of the bounds of the arrays return a fallback value fixed at array construction time, writing out of the bounds of the arrays is ignored.
Contrary to FallbackArray
, writing generates a fresh array.
Please notice that this implementation is naive and should only be used for small arrays. If there is a need for large functional arrays, it is recommended to implement Backer's trick to get constant-time reads and writes for sequences of mutations applied to the same array.
The type for array containing values of type 'a
.
make len v
builds an array a
initializing len
cells with v
. The value v
is the fallback value for a
.
init len v make
builds an array a
initializing len
cells where the i
-th cell value is make i
. The value v
is the fallback value for a
.
get a idx
returns the contents of the cell of index idx
in a
. If idx
< 0 or idx
>= length a
, get a idx
= fallback a
.
set a idx value
returns a new array identical to a
except that the cell of index idx
with value
. If idx
< 0 or idx
>= length a
, returns a copy of a
.
iter f a
iterates f
over the cells of a
from the cell indexed 0
to the cell indexed length a - 1
.
iteri f a
iterates f
over the cells of a
from the cell indexed 0
to the cell indexed length a - 1
passing the cell index to f
.
map a
computes a new array obtained by applying f
to each cell contents of a
. Notice that the fallback value of the new array is f (fallback a)
.
mapi f a
computes a new array obtained by applying f
to each cell contents of a
passing the index of this cell to i
. Notice that the fallback value of the new array is f (-1) (fallback a)
.
fold f a init
traverses a
from the cell indexed 0
to the cell indexed length a - 1
and transforms accu
into f accu x
where x
is the content of the cell under focus. accu
is init
on the first iteration.
fold_map f a init fallback
traverses a
from the cell indexed 0
to the cell indexed length a - 1
and transforms accu
into fst (f accu x)
where x
is the content of the cell under focus. accu
is init
on the first iteration. The function also returns a fresh array containing snd (f accu x)
for each x
. fallback
is required to initialize a fresh array before it can be filled.