diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-30 15:50:54 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-30 15:50:54 -0500 |
| commit | 40c33987fa0a91a8525d960f8494ca9ddf12806d (patch) | |
| tree | fa89233cbd7096de735348b39704e1ddb97dfe0c /docs | |
| parent | f3fc7558bb425c4bd59b55527d2fafaa744fe0aa (diff) | |
Bring back `table[key]` syntax
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/functions.md | 6 | ||||
| -rw-r--r-- | docs/tables.md | 59 |
2 files changed, 44 insertions, 21 deletions
diff --git a/docs/functions.md b/docs/functions.md index 05d84e4a..5ea2decd 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -86,10 +86,10 @@ add_cache := @{:add_args:Int} func add(x, y:Int -> Int): args := add_args(x, y) - if add_cache:has(args): - return add_cache:get(args) + if cached := add_cache[args]: + return cached ret := _add(x, y) - add_cache:set(args, ret) + add_cache[args] = ret return ret ``` diff --git a/docs/tables.md b/docs/tables.md index 6cbe859c..ef2333a7 100644 --- a/docs/tables.md +++ b/docs/tables.md @@ -5,11 +5,6 @@ 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 @@ -38,21 +33,29 @@ 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 +## Accessing 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: +Table values can be accessed with square bracket indexing. The result is an +optional value: ```tomo ->> t := {"x":1, "y":2} ->> t:get("x") +table := {"A": 1, "B": 2} +>> table["A"] = 1 : Int? ->> t:get("????") +>> table["missing"] = NONE : Int? ->> t:get("x")! -= 1 +``` + +As with all optional values, you can use the `!` postfix operator to assert +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 : Int + +>> table["missing"] or -1 += -1 : Int ``` ### Fallback Tables @@ -63,12 +66,32 @@ is not found in the table itself: ```tomo t := {"A": 10} t2 := {"B": 20; fallback=t} ->> t2:get("A") -= 10 +>> t2["A"] += 10 : Int? ``` The fallback is available by the `.fallback` field, which returns an optional -table value. +table value: + +```tomo +>> t2.fallback += {"A":10} : {Text:Int}? +>> t.fallback += NONE : {Text:Int}? +``` + +## Setting Values + +You can assign a new key/value mapping or overwrite an existing one using +`:set(key, value)` or an `=` assignment statement: + +```tomo +t := {"A": 1, "B": 2} +t["B"] = 222 +t["C"] = 333 +>> t += {"A":1, "B":222, "C":333} +``` ## Length |
