code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(270 lines)

% API

Builtins

Table

Table.clear

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:

t := &{"A":1}
t.clear()
assert t[] == {}

Table.difference

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:

t1 := {"A": 1, "B": 2, "C": 3}
t2 := {"B": 2, "C":30, "D": 40}
assert t1.difference(t2) == {"A": 1, "D": 40}

Table.get

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:

t := {"A": 1, "B": 2}
assert t.get("A") == 1
assert t.get("????") == none
assert t.get("A")! == 1
assert t.get("????") or 0 == 0

Table.get_or_set

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:

t := &{"A": @[1, 2, 3]; default=@[]}
t.get_or_set("A").insert(4)
t.get_or_set("B").insert(99)
assert t["A"][] == [1, 2, 3, 4]
assert t["B"][] == [99]
assert t.get_or_set("C", @[0, 0, 0])[] == [0, 0, 0]

Table.has

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:

assert {"A": 1, "B": 2}.has("A") == yes
assert {"A": 1, "B": 2}.has("xxx") == no

Table.intersection

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:

t1 := {"A": 1, "B": 2, "C": 3}
t2 := {"B": 2, "C":30, "D": 40}
assert t1.intersection(t2) == {"B": 2}

Table.remove

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:

t := &{"A": 1, "B": 2}
t.remove("A")
assert t == {"B": 2}

Table.set

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:

t := &{"A": 1, "B": 2}
t.set("C", 3)
assert t == {"A": 1, "B": 2, "C": 3}

Table.with

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:

t := {"A": 1, "B": 2}
assert t.with({"B": 20, "C": 30}) == {"A": 1, "B": 20, "C": 30}

Table.with_fallback

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:

t := {"A": 1; fallback={"B": 2}}
t2 := t.with_fallback({"B": 3})
assert t2["B"] == 3
t3 := t.with_fallback(none)
assert t3["B"] == none

Table.without

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:

t := {"A": 1, "B": 2, "C": 3}
assert t.without({"B": 2, "C": 30, "D": 40}) == {"A": 1, "C": 3}

1 % API
3 # Builtins
5 # Table
6 ## Table.clear
8 ```tomo
9 Table.clear : func(t: &{K:V} -> Void)
10 ```
12 Removes all key-value pairs from the table.
14 Argument | Type | Description | Default
15 ---------|------|-------------|---------
16 t | `&{K:V}` | The reference to the table. | -
18 **Return:** Nothing.
21 **Example:**
22 ```tomo
23 t := &{"A":1}
24 t.clear()
25 assert t[] == {}
27 ```
28 ## Table.difference
30 ```tomo
31 Table.difference : func(t: {K:V}, other: {K:V} -> {K:V})
32 ```
34 Return a table whose key/value pairs correspond to keys only present in one table, but not the other.
36 Argument | Type | Description | Default
37 ---------|------|-------------|---------
38 t | `{K:V}` | The base table. | -
39 other | `{K:V}` | The other table. | -
41 **Return:** A table containing the common key/value pairs whose keys only appear in one table.
44 **Example:**
45 ```tomo
46 t1 := {"A": 1, "B": 2, "C": 3}
47 t2 := {"B": 2, "C":30, "D": 40}
48 assert t1.difference(t2) == {"A": 1, "D": 40}
50 ```
51 ## Table.get
53 ```tomo
54 Table.get : func(t: {K:V}, key: K -> V?)
55 ```
57 Retrieves the value associated with a key, or returns `none` if the key is not present.
59 Default values for the table are ignored.
61 Argument | Type | Description | Default
62 ---------|------|-------------|---------
63 t | `{K:V}` | The table. | -
64 key | `K` | The key whose associated value is to be retrieved. | -
66 **Return:** The value associated with the key or `none` if the key is not found.
69 **Example:**
70 ```tomo
71 t := {"A": 1, "B": 2}
72 assert t.get("A") == 1
73 assert t.get("????") == none
74 assert t.get("A")! == 1
75 assert t.get("????") or 0 == 0
77 ```
78 ## Table.get_or_set
80 ```tomo
81 Table.get_or_set : func(t: &{K:V}, key: K, default: V -> V?)
82 ```
84 If the given key is in the table, return the associated value. Otherwise, insert the given default value into the table and return it.
86 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.
87 The default value is only evaluated if the key is missing.
89 Argument | Type | Description | Default
90 ---------|------|-------------|---------
91 t | `&{K:V}` | The table. | -
92 key | `K` | The key whose associated value is to be retrieved. | -
93 default | `V` | The default value to insert and return if the key is not present in the table. | -
95 **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.
98 **Example:**
99 ```tomo
100 t := &{"A": @[1, 2, 3]; default=@[]}
101 t.get_or_set("A").insert(4)
102 t.get_or_set("B").insert(99)
103 assert t["A"][] == [1, 2, 3, 4]
104 assert t["B"][] == [99]
105 assert t.get_or_set("C", @[0, 0, 0])[] == [0, 0, 0]
107 ```
108 ## Table.has
110 ```tomo
111 Table.has : func(t: {K:V}, key: K -> Bool)
112 ```
114 Checks if the table contains a specified key.
116 Argument | Type | Description | Default
117 ---------|------|-------------|---------
118 t | `{K:V}` | The table. | -
119 key | `K` | The key to check for presence. | -
121 **Return:** `yes` if the key is present, `no` otherwise.
124 **Example:**
125 ```tomo
126 assert {"A": 1, "B": 2}.has("A") == yes
127 assert {"A": 1, "B": 2}.has("xxx") == no
129 ```
130 ## Table.intersection
132 ```tomo
133 Table.intersection : func(t: {K:V}, other: {K:V} -> {K:V})
134 ```
136 Return a table with only the matching key/value pairs that are common to both tables.
138 Argument | Type | Description | Default
139 ---------|------|-------------|---------
140 t | `{K:V}` | The base table. | -
141 other | `{K:V}` | The other table. | -
143 **Return:** A table containing the common key/value pairs shared between two tables.
146 **Example:**
147 ```tomo
148 t1 := {"A": 1, "B": 2, "C": 3}
149 t2 := {"B": 2, "C":30, "D": 40}
150 assert t1.intersection(t2) == {"B": 2}
152 ```
153 ## Table.remove
155 ```tomo
156 Table.remove : func(t: {K:V}, key: K -> Void)
157 ```
159 Removes the key-value pair associated with a specified key.
161 Argument | Type | Description | Default
162 ---------|------|-------------|---------
163 t | `{K:V}` | The reference to the table. | -
164 key | `K` | The key of the key-value pair to remove. | -
166 **Return:** Nothing.
169 **Example:**
170 ```tomo
171 t := &{"A": 1, "B": 2}
172 t.remove("A")
173 assert t == {"B": 2}
175 ```
176 ## Table.set
178 ```tomo
179 Table.set : func(t: {K:V}, key: K, value: V -> Void)
180 ```
182 Sets or updates the value associated with a specified key.
184 Argument | Type | Description | Default
185 ---------|------|-------------|---------
186 t | `{K:V}` | The reference to the table. | -
187 key | `K` | The key to set or update. | -
188 value | `V` | The value to associate with the key. | -
190 **Return:** Nothing.
193 **Example:**
194 ```tomo
195 t := &{"A": 1, "B": 2}
196 t.set("C", 3)
197 assert t == {"A": 1, "B": 2, "C": 3}
199 ```
200 ## Table.with
202 ```tomo
203 Table.with : func(t: {K:V}, other: {K:V} -> {K:V})
204 ```
206 Return a copy of a table with values added from another table
208 Argument | Type | Description | Default
209 ---------|------|-------------|---------
210 t | `{K:V}` | The base table. | -
211 other | `{K:V}` | The other table from which new key/value pairs will be added. | -
213 **Return:** The original table, but with values from the other table added.
216 **Example:**
217 ```tomo
218 t := {"A": 1, "B": 2}
219 assert t.with({"B": 20, "C": 30}) == {"A": 1, "B": 20, "C": 30}
221 ```
222 ## Table.with_fallback
224 ```tomo
225 Table.with_fallback : func(t: {K:V}, fallback: {K:V}? -> {K:V})
226 ```
228 Return a copy of a table with a different fallback table.
230 Argument | Type | Description | Default
231 ---------|------|-------------|---------
232 t | `{K:V}` | The table whose fallback will be replaced. | -
233 fallback | `{K:V}?` | The new fallback table value. | -
235 **Return:** The original table with a different fallback.
238 **Example:**
239 ```tomo
240 t := {"A": 1; fallback={"B": 2}}
241 t2 := t.with_fallback({"B": 3})
242 assert t2["B"] == 3
243 t3 := t.with_fallback(none)
244 assert t3["B"] == none
246 ```
247 ## Table.without
249 ```tomo
250 Table.without : func(t: {K:V}, other: {K:V} -> {K:V})
251 ```
253 Return a copy of a table, but without any of the exact key/value pairs found in the other table.
255 Only exact key/value pairs will be discarded. Keys with a non-matching value will be kept.
257 Argument | Type | Description | Default
258 ---------|------|-------------|---------
259 t | `{K:V}` | The base table. | -
260 other | `{K:V}` | The other table whose key/value pairs will be omitted. | -
262 **Return:** The original table, but without the key/value pairs from the other table.
265 **Example:**
266 ```tomo
267 t := {"A": 1, "B": 2, "C": 3}
268 assert t.without({"B": 2, "C": 30, "D": 40}) == {"A": 1, "C": 3}
270 ```