aboutsummaryrefslogtreecommitdiff
path: root/test/tables.tm
blob: 6abbbe360c523f461a7c8b9d050fcb548e6a31a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
func main():
	>> t := {"one":1, "two":2}
	= {"one":1, "two":2}

	>> t["one"]
	= 1 : Int?
	>> t["two"]
	= 2 : Int?
	>> t["???"]
	= none : Int?
	>> t["one"]!
	= 1
	>> t["???"] or -1
	= -1

	t_str := ""
	for k,v in t:
		t_str ++= "($k:$v)"
	>> t_str
	= "(one:1)(two:2)"

	>> t.length
	= 2
	>> t.fallback
	= none : {Text:Int}?

	>> t.keys
	= ["one", "two"]
	>> t.values
	= [1, 2]

	>> t2 := {"three":3; fallback=t}
	= {"three":3; fallback={"one":1, "two":2}}

	>> t2["one"]
	= 1 : Int?
	>> t2["three"]
	= 3 : Int?
	>> t2["???"]
	= none : Int?

	>> t2.length
	= 1
	>> t2.fallback
	= {"one":1, "two":2} : {Text:Int}?

	t2_str := ""
	for k,v in t2:
		t2_str ++= "($k:$v)"
	>> t2_str
	= "(three:3)"

	>> {i:10*i for i in 5}
	= {1:10, 2:20, 3:30, 4:40, 5:50}
	>> {i:10*i for i in 5 if i mod 2 != 0}
	= {1:10, 3:30, 5:50}
	>> {x:10*x for x in y if x > 1 for y in [3, 4, 5] if y < 5}
	= {2:20, 3:30, 4:40}

	>> t3 := @{1:10, 2:20, 3:30}
	>> t3:remove(3)
	>> t3
	= @{1:10, 2:20}

	do:
		>> plain := {1:10, 2:20, 3:30}
		>> plain[2]!
		= 20
		>> plain[2]!
		= 20
		>> plain[456] or -999
		= -999
		>> plain:has(2)
		= yes
		>> plain:has(456)
		= no

		>> fallback := {4:40; fallback=plain}
		>> fallback:has(1)
		= yes
		>> fallback[1] or -999
		= 10

	do:
		>> t4 := &{"one": 1}
		>> t4["one"] = 999
		>> t4["two"] = 222
		>> t4
		= &{"one":999, "two":222}

	do:
		>> {1:1, 2:2} == {2:2, 1:1}
		= yes
		>> {1:1, 2:2} == {1:1, 2:999}
		= no

		>> {1:1, 2:2} <> {2:2, 1:1}
		= 0
		>> [{:Int:Int}, {0:0}, {99:99}, {1:1, 2:2, 3:3}, {1:1, 99:99, 3:3}, {1:1, 2:-99, 3:3}, {1:1, 99:-99, 3:4}]:sorted()
		= [{}, {0:0}, {1:1, 2:-99, 3:3}, {1:1, 2:2, 3:3}, {1:1, 99:99, 3:3}, {1:1, 99:-99, 3:4}, {99:99}]

		>> [{:Int}, {1}, {2}, {99}, {0, 3}, {1, 2}, {99}]:sorted()
		= [{}, {0, 3}, {1}, {1, 2}, {2}, {99}, {99}]