ligo/gitlab-pages/docs/reference/map.md
2020-03-17 15:38:41 +00:00

12 KiB

id title description hide_table_of_contents
map-reference Map Map operations 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).

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.

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).

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.

const empty : register = Map.empty

Or

const empty : register = map []
let empty : register = Map.empty
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.

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)];
let moves : register =
  Map.literal [
    (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address), (1,2));
    (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (0,3))]
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.

const my_balance : option (move) =
  Map.find_opt (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), moves)

Alternatively:

const my_balance_alternative : option (move) =
  moves [("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address)];
let my_balance : move option =
  Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) moves
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.

  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
let updated_map : register =
  Map.update
    ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (Some (4,9)) moves
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)
const added_item : register = Map.add (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address), (4, 9), moves)
let add (m : register) : register =
  Map.add
    ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN" : address) (4,9) m
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)
  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)
let updated_map : register =
  Map.remove ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) moves
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
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.

let iter_op (m : register) : unit =
  let predicate = fun (i,j : address * move) -> assert (j.0 > 3)
  in Map.iter predicate m
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)
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.

let map_op (m : register) : register =
  let increment = fun (i,j : address * move) -> j.0, j.1 + 1
  in Map.map increment m
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
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.

let fold_op (m : register) : int =
  let folded = fun (i,j : int * (address * move)) -> i + j.1.1
  in Map.fold folded m 5
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.