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
|
# Tables
Tables are Tomo's associative mapping structure, also known as a Dictionary or
Map. Tables are efficiently implemented as a hash table that preserves
insertion order and has fast access to keys and values as array slices. Tables
support *all* types as both keys and values.
Tables do not support square bracket indexing (`t[key]`), but instead rely on
the methods `:get(key)` and `:set(key, value)`. This is explicit to avoid
hiding the fact that table lookups and table insertion are performing function
calls and have edge conditions like a failure to find an entry.
## Syntax
Tables are written using `{}` curly braces with `:` colons associating key
expressions with value expressions and commas between entries:
```tomo
table := {"A": 10, "B": 20}
```
Empty tables must specify the key and value types explicitly:
```tomo
empty := {:Text:Int}
```
For type annotations, a table that maps keys with type `K` to values of type
`V` is written as `{K:V}`.
### Comprehensions
Similar to arrays, tables can use comprehensions to dynamically construct tables:
```tomo
t := {i: 10*i for i in 10}
t := {i: 10*i for i in 10 if i mod 2 == 0}
t := {-1:-10, i: 10*i for i in 10}
```
### Getting Values
To get a value from a table, use `:get(key)`, which returns an _optional_
value, depending on whether it was present in the table or not. For convenience,
you can use the `!` postifx operator to perform a check to ensure that the value
was found or error if it wasn't:
```tomo
>> t := {"x":1, "y":2}
>> t:get("x")
= 1?
>> t:get("????")
= !Int
>> t:get("x")!
= 1
```
### Fallback Tables
Tables can specify a fallback table that is used when looking up a value if it
is not found in the table itself:
```tomo
t := {"A": 10}
t2 := {"B": 20; fallback=t}
>> t2:get("A")
= 10
```
The fallback is available by the `.fallback` field, which returns an optional
table value.
## Length
Table length can be accessed by the `.length` field:
```tomo
>> {"A":10, "B":20}.length
= 2
```
## Accessing Keys and Values
The keys and values of a table can be efficiently accessed as arrays using a
constant-time immutable slice of the internal data from the table:
```tomo
t := {"A": 10, "B": 20}
>> t.keys
= ["A", "B"]
>> t.values
= [10, 20]
```
## Iteration
You can iterate over the key/value pairs in a table like this:
```tomo
for key, value in table:
...
for key in table:
...
```
Table iteration operates over the value of the table when the loop began, so
modifying the table during iteration is safe and will not result in the loop
iterating over any of the new values.
## Table Methods
### `bump`
**Description:**
Increments the value associated with a key by a specified amount. If the key is
not already in the table, its value will be assumed to be zero.
**Usage:**
```markdown
bump(t:{K:V}, key: K, amount: Int = 1) -> Void
```
**Parameters:**
- `t`: The mutable reference to the table.
- `key`: The key whose value is to be incremented.
- `amount`: The amount to increment the value by (default: 1).
**Returns:**
Nothing.
**Example:**
```markdown
>> t := {"A":1}
t:bump("A")
t:bump("B", 10)
>> t
= {"A": 2, "B": 10}
```
---
### `clear`
**Description:**
Removes all key-value pairs from the table.
**Usage:**
```markdown
t:clear() -> Void
```
**Parameters:**
- `t`: The mutable reference to the table.
**Returns:**
Nothing.
**Example:**
```markdown
>> t:clear()
```
---
### `get`
**Description:**
Retrieves the value associated with a key, or returns null if the key is not present.
**Usage:**
```markdown
t:get(key: K) -> V?
```
**Parameters:**
- `t`: The table.
- `key`: The key whose associated value is to be retrieved.
**Returns:**
The value associated with the key or null if the key is not found.
**Example:**
```markdown
>> t := {"A":1, "B":2}
>> t:get("A")
= 1? : Int?
>> t:get("????")
= !Int : Int?
>> t:get("A")!
= 1 : Int
>> t:get("????") or 0
= 0 : Int
```
---
### `has`
**Description:**
Checks if the table contains a specified key.
**Usage:**
```markdown
has(t:{K:V}, key: K) -> Bool
```
**Parameters:**
- `t`: The table.
- `key`: The key to check for presence.
**Returns:**
`yes` if the key is present, `no` otherwise.
**Example:**
```markdown
>> {"A":1, "B":2}:has("A")
= yes
>> {"A":1, "B":2}:has("xxx")
= no
```
---
### `remove`
**Description:**
Removes the key-value pair associated with a specified key.
**Usage:**
```markdown
remove(t:{K:V}, key: K) -> Void
```
**Parameters:**
- `t`: The mutable reference to the table.
- `key`: The key of the key-value pair to remove.
**Returns:**
Nothing.
**Example:**
```markdown
t := {"A":1, "B":2}
t:remove("A")
>> t
= {"B": 2}
```
---
### `set`
**Description:**
Sets or updates the value associated with a specified key.
**Usage:**
```markdown
set(t:{K:V}, key: K, value: V) -> Void
```
**Parameters:**
- `t`: The mutable reference to the table.
- `key`: The key to set or update.
- `value`: The value to associate with the key.
**Returns:**
Nothing.
**Example:**
```markdown
t := {"A": 1, "B": 2}
t:set("C", 3)
>> t
= {"A": 1, "B": 2, "C": 3}
```
|