% 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 % API3 # Builtins5 # Table6 ## Table.clear9 Table.clear : func(t: &{K:V} -> Void)10 ```12 Removes all key-value pairs from the table.14 Argument | Type | Description | Default15 ---------|------|-------------|---------21 **Example:**23 t := &{"A":1}24 t.clear()25 assert t[] == {}27 ```28 ## Table.difference31 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 | Default37 ---------|------|-------------|---------44 **Example:**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.get54 Table.get : func(t: {K:V}, key: K -> V?)55 ```59 Default values for the table are ignored.61 Argument | Type | Description | Default62 ---------|------|-------------|---------69 **Example:**71 t := {"A": 1, "B": 2}72 assert t.get("A") == 173 assert t.get("????") == none74 assert t.get("A")! == 175 assert t.get("????") or 0 == 077 ```78 ## Table.get_or_set81 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 | Default90 ---------|------|-------------|---------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:**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.has111 Table.has : func(t: {K:V}, key: K -> Bool)112 ```114 Checks if the table contains a specified key.116 Argument | Type | Description | Default117 ---------|------|-------------|---------124 **Example:**126 assert {"A": 1, "B": 2}.has("A") == yes127 assert {"A": 1, "B": 2}.has("xxx") == no129 ```130 ## Table.intersection133 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 | Default139 ---------|------|-------------|---------146 **Example:**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.remove156 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 | Default162 ---------|------|-------------|---------169 **Example:**171 t := &{"A": 1, "B": 2}172 t.remove("A")173 assert t == {"B": 2}175 ```176 ## Table.set179 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 | Default185 ---------|------|-------------|---------193 **Example:**195 t := &{"A": 1, "B": 2}196 t.set("C", 3)197 assert t == {"A": 1, "B": 2, "C": 3}199 ```200 ## Table.with203 Table.with : func(t: {K:V}, other: {K:V} -> {K:V})204 ```206 Return a copy of a table with values added from another table208 Argument | Type | Description | Default209 ---------|------|-------------|---------216 **Example:**218 t := {"A": 1, "B": 2}219 assert t.with({"B": 20, "C": 30}) == {"A": 1, "B": 20, "C": 30}221 ```222 ## Table.with_fallback225 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 | Default231 ---------|------|-------------|---------238 **Example:**240 t := {"A": 1; fallback={"B": 2}}241 t2 := t.with_fallback({"B": 3})242 assert t2["B"] == 3243 t3 := t.with_fallback(none)244 assert t3["B"] == none246 ```247 ## Table.without250 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 | Default258 ---------|------|-------------|---------265 **Example:**267 t := {"A": 1, "B": 2, "C": 3}268 assert t.without({"B": 2, "C": 30, "D": 40}) == {"A": 1, "C": 3}270 ```