Version: 0.9.0
Map
function empty : map ('key, 'value)
Create an empty map.
type move is int * int
type register is map (address, move)
const empty : register = Map.empty
Or
const empty : register = map []
function literal : list ('key * 'value) -> map ('key, 'value)
Create a non-empty map.
const moves : register =
Map.literal (list [
(("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]);
Alternative way of creating an empty map:
const moves_alternative : register =
map [
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2);
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)];
function find_opt : 'key -> map ('key, 'value) -> option 'value
Retrieve a (option) value from a map with the given key. Returns None
if the
key is missing and the value otherwise.
const my_balance : option (move) =
Map.find_opt (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), moves)
Alternatively:
const my_balance_alternative : option (move) =
moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)];
function update : 'key -> option 'value -> map ('key, 'value) -> map ('key, 'value)
Note: when None
is used as a value, the key and associated value is removed
from the map.
const updated_map : register = Map.update(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some (4,9), moves);
Alternatively:
function update (var m : register) : register is
block {
m [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)] := (4,9);
} with m
If multiple bindings need to be updated, PascaLIGO offers a patch instruction for maps, similar to that for records.
function assignments (var m : register) : register is
block {
patch m with map [
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (4,9);
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2)
]
} with m
function add : 'key -> 'value -> map ('key, 'value) -> map ('key, 'value)
const added_item : register = Map.add (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4, 9), moves)
function remove : 'key -> map ('key, 'value) -> map ('key, 'value)
const updated_map : register =
Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves)
Alternatively, the instruction remove key from map m
removes the key
key
from the map m
.
function rem (var m : register) : register is
block {
remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) from map moves
} with m
const updated_map : register = rem (moves)
function iter : ((key, value) -> unit) -> map (key, value) -> unit
function iter_op (const m : register) : unit is
block {
function iterated (const i : address; const j : move) : unit is
if j.1 > 3 then Unit else (failwith ("Below range.") : unit)
} with Map.iter (iterated, m)
Note that
map_iter
is deprecated.
function map : (('key, 'value) -> ('mapped_key, 'mapped_item)) -> map ('key, 'value) -> map ('mapped_key, 'mapped_value)
function map_op (const m : register) : register is
block {
function increment (const i : address; const j : move) : move is
(j.0, j.1 + 1)
} with Map.map (increment, m)
Note that
map_map
is deprecated.
function fold : (('accumulator -> ('key, 'value) -> 'accumulator) -> map ('key, 'value) -> 'accumulator) -> 'accumulator
function fold_op (const m : register) : int is
block {
function folded (const i : int; const j : address * move) : int is
i + j.1.1
} with Map.fold (folded, m, 5)
Note that
map_fold
is deprecated.
function size : map ('key, 'value) -> nat
Returns the number of items in the map.
function mem : key -> map (key, value) -> bool
Checks if a key exists in the map.