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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
% API
# Builtins
# Table
## Table.clear
```tomo
Table.clear : func(t: &{K:V} -> Void)
```
Removes all key-value pairs from the table.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `&{K:V}` | The reference to the table. | -
**Return:** Nothing.
**Example:**
```tomo
>> t.clear()
```
## Table.difference
```tomo
Table.difference : func(t: {K:V}, other: {K:V} -> {K:V})
```
Return a table whose key/value pairs correspond to keys only present in one table, but not the other.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The base table. | -
other | `{K:V}` | The other table. | -
**Return:** A table containing the common key/value pairs whose keys only appear in one table.
**Example:**
```tomo
t1 := {"A": 1; "B": 2, "C": 3}
t2 := {"B": 2, "C":30, "D": 40}
assert t1.difference(t2) == {"A": 1, "D": 40}
```
## Table.get
```tomo
Table.get : func(t: {K:V}, key: K -> V?)
```
Retrieves the value associated with a key, or returns `none` if the key is not present.
Default values for the table are ignored.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The table. | -
key | `K` | The key whose associated value is to be retrieved. | -
**Return:** The value associated with the key or `none` if the key is not found.
**Example:**
```tomo
>> t := {"A": 1, "B": 2}
>> t.get("A")
= 1?
>> t.get("????")
= none
>> t.get("A")!
= 1
>> t.get("????") or 0
= 0
```
## Table.get_or_set
```tomo
Table.get_or_set : func(t: &{K:V}, key: K, default: V -> V?)
```
If the given key is in the table, return the associated value. Otherwise, insert the given default value into the table and return it.
If no default value is provided explicitly, but the table has a default value associated with it, the table's default value will be used.
The default value is only evaluated if the key is missing.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `&{K:V}` | The table. | -
key | `K` | The key whose associated value is to be retrieved. | -
default | `V` | The default value to insert and return if the key is not present in the table. | -
**Return:** Either the value associated with the key (if present) or the default value. The table will be mutated if the key is not already present.
**Example:**
```tomo
>> t := &{"A": @[1, 2, 3]; default=@[]}
>> t.get_or_set("A").insert(4)
>> t.get_or_set("B").insert(99)
>> t
= &{"A": @[1, 2, 3, 4], "B": @[99]}
>> t.get_or_set("C", @[0, 0, 0])
= @[0, 0, 0]
>> t
= &{"A": @[1, 2, 3, 4], "B": @[99], "C": @[0, 0, 0]}
```
## Table.has
```tomo
Table.has : func(t: {K:V}, key: K -> Bool)
```
Checks if the table contains a specified key.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The table. | -
key | `K` | The key to check for presence. | -
**Return:** `yes` if the key is present, `no` otherwise.
**Example:**
```tomo
>> {"A": 1, "B": 2}.has("A")
= yes
>> {"A": 1, "B": 2}.has("xxx")
= no
```
## Table.intersection
```tomo
Table.intersection : func(t: {K:V}, other: {K:V} -> {K:V})
```
Return a table with only the matching key/value pairs that are common to both tables.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The base table. | -
other | `{K:V}` | The other table. | -
**Return:** A table containing the common key/value pairs shared between two tables.
**Example:**
```tomo
t1 := {"A": 1; "B": 2, "C": 3}
t2 := {"B": 2, "C":30, "D": 40}
assert t1.intersection(t2) == {"B": 2}
```
## Table.remove
```tomo
Table.remove : func(t: {K:V}, key: K -> Void)
```
Removes the key-value pair associated with a specified key.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The reference to the table. | -
key | `K` | The key of the key-value pair to remove. | -
**Return:** Nothing.
**Example:**
```tomo
t := {"A": 1, "B": 2}
t.remove("A")
>> t
= {"B": 2}
```
## Table.set
```tomo
Table.set : func(t: {K:V}, key: K, value: V -> Void)
```
Sets or updates the value associated with a specified key.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The reference to the table. | -
key | `K` | The key to set or update. | -
value | `V` | The value to associate with the key. | -
**Return:** Nothing.
**Example:**
```tomo
t := {"A": 1, "B": 2}
t.set("C", 3)
>> t
= {"A": 1, "B": 2, "C": 3}
```
## Table.with
```tomo
Table.with : func(t: {K:V}, other: {K:V} -> {K:V})
```
Return a copy of a table with values added from another table
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The base table. | -
other | `{K:V}` | The other table from which new key/value pairs will be added. | -
**Return:** The original table, but with values from the other table added.
**Example:**
```tomo
t := {"A": 1; "B": 2}
assert t.with({"B": 20, "C": 30}) == {"A": 1, "B": 20, "C": 30}
```
## Table.with_fallback
```tomo
Table.with_fallback : func(t: {K:V}, fallback: {K:V}? -> {K:V})
```
Return a copy of a table with a different fallback table.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The table whose fallback will be replaced. | -
fallback | `{K:V}?` | The new fallback table value. | -
**Return:** The original table with a different fallback.
**Example:**
```tomo
t := {"A": 1; fallback={"B": 2}}
t2 = t.with_fallback({"B": 3"})
>> t2["B"]
= 3?
t3 = t.with_fallback(none)
>> t2["B"]
= none
```
## Table.without
```tomo
Table.without : func(t: {K:V}, other: {K:V} -> {K:V})
```
Return a copy of a table, but without any of the exact key/value pairs found in the other table.
Only exact key/value pairs will be discarded. Keys with a non-matching value will be kept.
Argument | Type | Description | Default
---------|------|-------------|---------
t | `{K:V}` | The base table. | -
other | `{K:V}` | The other table whose key/value pairs will be omitted. | -
**Return:** The original table, but without the key/value pairs from the other table.
**Example:**
```tomo
t := {"A": 1; "B": 2, "C": 3}
assert t.without({"B": 2, "C": 30, "D": 40}) == {"A": 1, "C": 3}
```
|