aboutsummaryrefslogtreecommitdiff
path: root/docs/tables.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tables.md')
-rw-r--r--docs/tables.md67
1 files changed, 39 insertions, 28 deletions
diff --git a/docs/tables.md b/docs/tables.md
index eaf0083e..12b2eb35 100644
--- a/docs/tables.md
+++ b/docs/tables.md
@@ -40,10 +40,8 @@ optional value:
```tomo
table := {"A": 1, "B": 2}
->> table["A"]
-= 1?
->> table["missing"]
-= none
+assert table["A"] == 1
+assert table["missing"] == none
```
As with all optional values, you can use the `!` postfix operator to assert
@@ -51,11 +49,9 @@ that the value is non-none (and create a runtime error if it is), or you can
use the `or` operator to provide a fallback value in the case that it's none:
```tomo
->> table["A"]!
-= 1
+assert table["A"]! == 1
->> table["missing"] or -1
-= -1
+assert (table["missing"] or -1) == -1
```
### Fallback Tables
@@ -66,18 +62,15 @@ is not found in the table itself:
```tomo
t := {"A": 10}
t2 := {"B": 20; fallback=t}
->> t2["A"]
-= 10?
+assert t2["A"] == 10
```
The fallback is available by the `.fallback` field, which returns an optional
table value:
```tomo
->> t2.fallback
-= {"A": 10}?
->> t.fallback
-= none
+assert t2.fallback == {"A": 10}
+assert t.fallback == none
```
### Default Values
@@ -87,13 +80,10 @@ present in the table or its fallback (if any).
```tomo
counts := &{"foo": 12; default=0}
->> counts["foo"]
-= 12
->> counts["baz"]
-= 0
+assert counts["foo"] == 12
+assert counts["baz"] == 0
counts["baz"] += 1
->> counts["baz"]
-= 1
+assert counts["baz"] == 1
```
When values are accessed from a table with a default value, the return type
@@ -108,8 +98,7 @@ You can assign a new key/value mapping or overwrite an existing one using
t := {"A": 1, "B": 2}
t["B"] = 222
t["C"] = 333
->> t
-= {"A": 1, "B": 222, "C": 333}
+assert t == {"A": 1, "B": 222, "C": 333}
```
## Length
@@ -117,8 +106,7 @@ t["C"] = 333
Table length can be accessed by the `.length` field:
```tomo
->> {"A": 10, "B": 20}.length
-= 2
+assert {"A": 10, "B": 20}.length == 2
```
## Accessing Keys and Values
@@ -128,10 +116,8 @@ 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]
+assert t.keys == ["A", "B"]
+assert t.values == [10, 20]
```
## Iteration
@@ -150,6 +136,31 @@ 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.
+## Sets
+
+For an interface similar to Python's Sets, Tomo tables can be used with an
+empty struct as its value type. For convenience, if a value or value is
+omitted, Tomo will assign a default value type of `struct Present()` (an empty
+struct). This way, the values stored in the table take up no space, but you
+still have an easy way to represent Set-like data.
+
+```tomo
+nums := {10, 20, 30, 10}
+assert nums.items == [10, 20, 30]
+assert nums[10] == Present()
+assert nums[99] == none
+```
+
+The following set-theoretic operations are available for tables:
+
+- Set union: (AKA `or`) `{10, 20, 30}.with({30, 40})` -> `{10, 20, 30, 40}`
+- Set intersection (AKA `and`) `{10, 20, 30}.intersection({30, 40})` -> `{10,
+ 20, 30, 40}`
+- Set difference (AKA, `xor`, disjunctive union, symmetric difference) `{10,
+ 20, 30}.difference({30, 40})` -> `{10, 20, 40}`
+- Set subtraction (AKA, `-`, asymmetric difference) `{10, 20, 30}.without({30,
+ 40})` -> `{10, 20}`
+
# API
[API documentation](../api/tables.md)