--- id: map-reference title: Map description: Map operations hide_table_of_contents: true --- import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle'; type map ('key, 'value) type ('key, 'value) map type map ('key, 'value) The type of a map from values of type `key` to values of type `value` is `map (key, value)`. ```pascaligo group=maps type move is int * int type register is map (address, move) ``` The type of a map from values of type `key` to values of type `value` is `(key, value) map`. ```cameligo group=maps type move = int * int type register = (address, move) map ``` The type of a map from values of type `key` to values of type `value` is `map (key, value)`. ```reasonligo group=maps type move = (int, int); type register = map (address, move); ``` function empty : map ('key, 'value) val empty : ('key, 'value) map let empty: map('key, 'value) Create an empty map. ```pascaligo group=maps const empty : register = Map.empty ``` Or ```pascaligo group=maps const empty : register = map [] ``` ```cameligo group=maps let empty : register = Map.empty ``` ```reasonligo group=maps let empty : register = Map.empty ``` function literal : list ('key * 'value) -> map ('key, 'value) val literal : ('key * 'value) list -> ('key, 'value) map let literal: list(('key, 'value)) => map('key, 'value) Create a non-empty map. ```pascaligo group=maps const moves : register = Map.literal (list [ (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2)); (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]); ``` Alternative way of creating an empty map: ```pascaligo group=maps const moves_alternative : register = map [ ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2); ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (0,3)]; ``` ```cameligo group=maps let moves : register = Map.literal [ (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2)); (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))] ``` ```reasonligo group=maps let moves : register = Map.literal ([ ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address, (1,2)), ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, (0,3))]); ``` function find_opt : 'key -> map ('key, 'value) -> option 'value val find_opt : 'key -> ('key, 'value) map -> 'value option let 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. ```pascaligo group=maps const my_balance : option (move) = Map.find_opt (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), moves) ``` Alternatively: ```pascaligo group=maps const my_balance_alternative : option (move) = moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)]; ``` ```cameligo group=maps let my_balance : move option = Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) moves ``` ```reasonligo group=maps let my_balance : option (move) = Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address, moves); ``` function update : 'key -> option 'value -> map ('key, 'value) -> map ('key, 'value) val update: 'key -> 'value option -> ('key, 'value) map -> ('key, 'value) map let 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. ```pascaligo group=maps const updated_map : register = Map.update(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some (4,9), moves); ``` Alternatively: ```pascaligo group=maps 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. ```pascaligo group=maps function assignments (var m : register) : register is block { patch m with map [ ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) -> (4,9); ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address) -> (1,2) ] } with m ``` ```cameligo group=maps let updated_map : register = Map.update ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves ``` ```reasonligo group=maps let updated_map : register = Map.update (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), Some ((4,9)), moves); ``` function add : 'key -> 'value -> map ('key, 'value) -> map ('key, 'value) val add : 'key -> 'value -> ('key, 'value) map -> ('key, 'value) map let add: ('key, 'value, map('key, 'value)) => map('key, 'value) ```pascaligo group=maps const added_item : register = Map.add (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4, 9), moves) ``` ```cameligo group=maps let add (m : register) : register = Map.add ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (4,9) m ``` ```reasonligo group=maps let add = (m: register): register => Map.add (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4,9), m); ``` function remove : 'key -> map ('key, 'value) -> map ('key, 'value) val remove : 'key -> ('key, 'value) map -> ('key, 'value) map let remove: (key, map('key, 'value)) => map('key, 'value) ```pascaligo group=maps 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`. ```pascaligo group=maps function rem (var m : register) : register is block { remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) from map moves } with m const updated_map : register = rem (moves) ``` ```cameligo group=maps let updated_map : register = Map.remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves ``` ```reasonligo group=maps let updated_map : register = Map.remove (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), moves) ``` function iter : ((key, value) -> unit) -> map (key, value) -> unit val iter : (('key * 'value) -> unit) -> ('key, 'value) map -> unit let iter: ((('key, 'value)) => unit, map('key, 'value)) => unit ```pascaligo group=maps 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*. ```cameligo group=maps let iter_op (m : register) : unit = let predicate = fun (i,j : address * move) -> assert (j.0 > 3) in Map.iter predicate m ``` ```reasonligo group=maps let iter_op = (m : register) : unit => { let predicate = ((i,j) : (address, move)) => assert (j[0] > 3); Map.iter (predicate, m); }; ``` function map : (('key, 'value) -> ('mapped_key, 'mapped_item)) -> map ('key, 'value) -> map ('mapped_key, 'mapped_value) val map : (('key * 'value) -> ('mapped_key * 'mapped_item)) -> (key, value) map -> (mapped_key, mapped_value) map let map: ((('key, 'value)) => ('mapped_key, 'mapped_item), map(key, value)) => map(mapped_key, mapped_value) ```pascaligo group=maps 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*. ```cameligo group=maps let map_op (m : register) : register = let increment = fun (i,j : address * move) -> j.0, j.1 + 1 in Map.map increment m ``` ```reasonligo group=maps let map_op = (m : register) : register => { let increment = ((i,j): (address, move)) => (j[0], j[1] + 1); Map.map (increment, m); }; ``` function fold : (('accumulator -> ('key, 'value) -> 'accumulator) -> map ('key, 'value) -> 'accumulator) -> 'accumulator val fold : ('accumulator -> ('key * 'value) -> 'accumulator) -> ('key, 'value) map -> 'accumulator -> 'accumulator let fold: ((('accumulator, ('key, 'value)) => 'accumulator), map('key, 'value), 'accumulator) => 'accumulator ```pascaligo group=maps 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*. ```cameligo group=maps let fold_op (m : register) : int = let folded = fun (i,j : int * (address * move)) -> i + j.1.1 in Map.fold folded m 5 ``` ```reasonligo group=maps let fold_op = (m : register) : int => { let folded = ((i,j): (int, (address, move))) => i + j[1][1]; Map.fold (folded, m, 5); }; ``` function size : map ('key, 'value) -> nat val size : ('key, 'value) map -> nat let size: map('key, 'value) => nat Returns the number of items in the map. function mem : key -> map (key, value) -> bool val mem : 'key -> ('key, 'value) map => bool let mem : ('key, map('key, 'value)) => bool Checks if a key exists in the map.