diff options
Diffstat (limited to 'docs/tables.md')
| -rw-r--r-- | docs/tables.md | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/docs/tables.md b/docs/tables.md index eaf0083e..00e3e8c0 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 |
