2019-09-21 14:59:48 -07:00
|
|
|
// Test map type and related built-in functions in PascaLIGO
|
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
type foobar is map (int, int)
|
|
|
|
|
|
|
|
const empty_map : foobar = map []
|
|
|
|
|
|
|
|
const map1 : foobar = map [
|
|
|
|
144 -> 23;
|
|
|
|
51 -> 23;
|
|
|
|
42 -> 23;
|
|
|
|
120 -> 23;
|
|
|
|
421 -> 23]
|
|
|
|
|
|
|
|
const map2 : foobar = map [23 -> 0; 42 -> 0]
|
|
|
|
|
|
|
|
function set_ (var n : int; var m : foobar) : foobar is block {
|
|
|
|
m[23] := n
|
2019-05-12 20:56:22 +00:00
|
|
|
} with m
|
|
|
|
|
2020-01-31 17:13:22 -08:00
|
|
|
function add (var n : int ; var m : foobar) : foobar is set_(n,m)
|
2019-05-12 20:56:22 +00:00
|
|
|
|
|
|
|
function rm (var m : foobar) : foobar is block {
|
|
|
|
remove 42 from map m
|
|
|
|
} with m
|
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function patch_ (var m : foobar) : foobar is block {
|
2019-10-10 18:26:28 -07:00
|
|
|
patch m with map [0 -> 5; 1 -> 6; 2 -> 7]
|
|
|
|
} with m
|
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function patch_deep (var m : foobar * nat) : foobar * nat is
|
2020-02-20 05:25:30 +00:00
|
|
|
block { patch m.0 with map [1 -> 9] } with m
|
2019-10-14 10:19:18 -07:00
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function size_ (const m : foobar) : nat is size (m)
|
2019-05-12 20:56:22 +00:00
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function gf (const m : foobar) : int is get_force (23, m)
|
2019-05-12 20:56:22 +00:00
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function get (const m : foobar) : option (int) is m[42]
|
2019-05-12 20:56:22 +00:00
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function get_ (const m : foobar) : option (int) is map_get (42, m)
|
2019-09-24 14:29:18 +02:00
|
|
|
|
2020-02-10 19:27:58 +01:00
|
|
|
function mem (const k: int; const m: foobar) : bool is map_mem (k, m)
|
2019-12-06 09:35:08 -08:00
|
|
|
|
2019-09-27 15:52:40 +00:00
|
|
|
function iter_op (const m : foobar) : unit is
|
2019-11-18 16:10:48 +01:00
|
|
|
block {
|
2020-02-10 19:27:58 +01:00
|
|
|
function aggregate (const i : int; const j : int) : unit is block
|
|
|
|
{ if i=j then skip else failwith ("fail") } with unit
|
|
|
|
} with map_iter (aggregate, m)
|
2019-07-20 16:42:34 +02:00
|
|
|
|
|
|
|
function map_op (const m : foobar) : foobar is
|
2019-11-18 16:10:48 +01:00
|
|
|
block {
|
2020-02-10 19:27:58 +01:00
|
|
|
function increment (const i : int; const j : int) : int is j+1
|
|
|
|
} with map_map (increment, m)
|
2019-09-23 23:46:47 +02:00
|
|
|
|
|
|
|
function fold_op (const m : foobar) : int is
|
2019-11-18 16:10:48 +01:00
|
|
|
block {
|
2020-02-10 19:27:58 +01:00
|
|
|
function aggregate (const i : int; const j : int * int) : int is
|
2020-02-20 05:25:30 +00:00
|
|
|
i + j.0 + j.1
|
2020-02-10 19:27:58 +01:00
|
|
|
} with map_fold(aggregate, m, 10)
|
2019-10-08 16:41:47 +02:00
|
|
|
|
|
|
|
function deep_op (var m : foobar) : foobar is
|
2019-11-18 16:10:48 +01:00
|
|
|
block {
|
2020-02-10 19:27:58 +01:00
|
|
|
var coco : int * foobar := (0, m);
|
2020-02-20 05:25:30 +00:00
|
|
|
remove 42 from map coco.1;
|
|
|
|
coco.1[32] := 16
|
|
|
|
} with coco.1
|