Skip to main content

Map

cameligo

val empty : ('key, 'value) map

jsligo

let empty: map<'key, 'value>

Create an empty map.

cameligo
type move = int * int
type register = (address, move) map

let empty : register = Map.empty
jsligo
type move = [int, int];
type register = map<address, move>;

let empty: register = Map.empty;
cameligo

val literal : ('key * 'value) list -> ('key, 'value) map

jsligo

let literal: (input: list<['key, 'value]>) => map<'key, 'value>

Create a non-empty map.

cameligo
let moves : register =
Map.literal [
(("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]
jsligo
let moves: register =
Map.literal(list([
[("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address), [1, 2]],
[("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), [0, 3]]]));
cameligo

val find_opt : 'key -> ('key, 'value) map -> 'value option

jsligo

let find_opt : (key: 'key, map: 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.

cameligo
let my_balance : move option =
Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) moves
jsligo
let my_balance: option<move> =
Map.find_opt(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), moves);
cameligo

val update: 'key -> 'value option -> ('key, 'value) map -> ('key, 'value) map

jsligo

let update: (key: 'key, new_value: option<'value>, map: map<'key, 'value>) => map <'key, 'value>

Note: When None is used as a value, the key and associated value is removed from the map.

cameligo
let updated_map : register =
Map.update
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves
jsligo
let updated_map : register =
Map.update
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), Some ([4, 9]), moves);
cameligo

val get_and_update : 'key -> 'value option -> ('key, 'value) map -> 'value option * ('key, 'value) map

jsligo

let get_and_update : (key : 'key, value : option<'value>, map : map<'key, 'value>) => [option<'value>, map<'key, 'value>]

Similar to update but it also returns the value that was previously stored in the map.

cameligo
let (old_move_opt, updated_map) : move option * register =
Map.get_and_update ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4, 9)) moves
jsligo
let [old_move, updated_map2] : [option<move>, register] =
Map.get_and_update (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), (Some([24, 48] as move)), moves);
cameligo

val add : 'key -> 'value -> ('key, 'value) map -> ('key, 'value) map

jsligo

let add: (key: 'key, value: 'value, map: map<'key, 'value>) => map<'key, 'value>

Returns a new map with key-value pair added to the input map.

cameligo
let add (m : register) : register =
Map.add
("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (4,9) m
jsligo
let add = (m: register): register =>
Map.add
(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), [4, 9], m);
cameligo

val remove : 'key -> ('key, 'value) map -> ('key, 'value) map

jsligo

let remove: ('key: key, map: map<'key, 'value>) => map<'key, 'value>

Returns a new map with key-value pair removed from the input map.

cameligo
let updated_map : register =
Map.remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves
jsligo
let updated_map3 : register =
Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" as address), moves);
cameligo

val iter : ('key * 'value -> unit) -> ('key, 'value) map -> unit

jsligo

let iter: (iter: (['key, 'value]) => unit, map: map<'key, 'value>) => unit

Iterate over key-value pairs in a map.

cameligo
let iter_op (m : register) : unit =
let predicate = fun (i,j : address * move) -> assert (j.0 > 3)
in Map.iter predicate m
jsligo
let iter_op = (m : register) : unit => {
let predicate = ([i, j]: [address, move]): unit => assert (j[0] > 3);
Map.iter (predicate, m);
};
cameligo

val map : ('key * 'value -> 'new_value) -> ('key, 'value) map -> ('key, 'new_value) map

jsligo

let map: (mapper: (item: ['key, 'value]) => 'new_value, map: map<'key, 'value>) => map<'key, 'new_value>

Applies the mapper function on the key-value pairs of map and builds a new map

cameligo
let map_op (m : register) : register =
let increment = fun (_i,j : address * move) -> j.0, j.1 + 1
in Map.map increment m
jsligo
let map_op = (m : register) : register => {
let increment = ([_i, j]: [address, move]) : move => [j[0], j[1] + 1];
return Map.map (increment, m);
};
cameligo

val fold : ('acc * ('key * 'value) -> 'acc) -> ('key, 'value) map -> 'acc -> 'acc

jsligo

let fold: (iter: ((acc: 'acc, item: ['key, 'value]) => 'acc), map: map<'key, 'value>, acc: 'acc) => 'acc

Fold over key-value pairs in a map:

cameligo
let fold_op (m : register) : int =
let folded = fun (i,j : int * (address * move)) -> i + j.1.1
in Map.fold folded m 5
jsligo
let fold_op = (m : register): int => {
let folded = ([i, j]: [int, [address, move]]):int => i + j[1][1];
return Map.fold (folded, m, 5);
};
cameligo

val size : ('key, 'value) map -> nat

jsligo

let size: (map: map<'key, 'value>) => nat

Return the number of items in the map:

cameligo
let _ : nat = Map.size moves
jsligo
let _ : nat = Map.size(moves);
cameligo

val mem : 'key -> ('key, 'value) map -> bool

jsligo

let mem : (key: 'key, map: map<'key, 'value>) => bool

Checks if a key exists in the map:

cameligo
let found : bool = Map.mem ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)  moves
jsligo
let found : bool = Map.mem (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" as address),  moves);