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 /examples | |
| parent | f3fc7558bb425c4bd59b55527d2fafaa744fe0aa (diff) | |
Bring back `table[key]` syntax
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/ini/ini.tm | 8 | ||||
| -rw-r--r-- | examples/learnxiny.tm | 16 | ||||
| -rw-r--r-- | examples/tomodeps/tomodeps.tm | 8 |
3 files changed, 16 insertions, 16 deletions
diff --git a/examples/ini/ini.tm b/examples/ini/ini.tm index 5952bb00..9575b9fd 100644 --- a/examples/ini/ini.tm +++ b/examples/ini/ini.tm @@ -20,11 +20,11 @@ func parse_ini(path:Path -> {Text:{Text:Text}}): if line:matches($/[?]/): section_name := line:replace($/[?]/, "\1"):trim():lower() current_section = @{:Text:Text} - sections:set(section_name, current_section) + sections[section_name] = current_section else if line:matches($/{..}={..}/): key := line:replace($/{..}={..}/, "\1"):trim():lower() value := line:replace($/{..}={..}/, "\2"):trim() - current_section:set(key, value) + current_section[key] = value return {k:v[] for k,v in sections[]} @@ -42,7 +42,7 @@ func main(path:Path, key:Text?): return section := keys[1]:lower() - section_data := data:get(section) or exit(" + section_data := data[section] or exit(" Invalid section name: $\[31;1]$section$\[] Valid names: $\[1]$(", ":join([k:quoted() for k in data.keys]))$\[] ") @@ -51,7 +51,7 @@ func main(path:Path, key:Text?): return section_key := keys[2]:lower() - value := section_data:get(section_key) or exit(" + value := section_data[section_key] or exit(" Invalid key: $\[31;1]$section_key$\[] Valid keys: $\[1]$(", ":join([s:quoted() for s in section_data.keys]))$\[] ") diff --git a/examples/learnxiny.tm b/examples/learnxiny.tm index e31bab70..af7a55f6 100644 --- a/examples/learnxiny.tm +++ b/examples/learnxiny.tm @@ -105,21 +105,21 @@ func main(): # Tables are efficient hash maps table := {"one": 1, "two": 2} - >> table:get("two") + >> table["two"] = 2 : Int? # The value returned is optional because NONE will be returned if the key # is not in the table: - >> table:get("xxx")! + >> table["xxx"]! = NONE : Int? # Optional values can be converted to regular values using `!` (which will # create a runtime error if the value is null): - >> table:get("two")! + >> table["two"]! = 2 : Int # You can also use `or` to provide a fallback value to replace NONE: - >> table:get("xxx") or 0 + >> table["xxx"] or 0 = 0 : Int # Empty tables require specifying the key and value types: @@ -142,9 +142,9 @@ func main(): # Tables can have a fallback table that's used as a fallback when the key # isn't found in the table itself: table2 := {"three": 3; fallback=table} - >> table2:get("two")! + >> table2["two"]! = 2 - >> table2:get("three")! + >> table2["three"]! = 3 # Tables can also be created with comprehension loops: @@ -157,7 +157,7 @@ func main(): # Any types can be used in tables, for example, a table mapping arrays to # strings: table3 := {[10, 20]: "one", [30, 40, 50]: "two"} - >> table3:get([10, 20])! + >> table3[[10, 20]]! = "one" # Sets are similar to tables, but they represent an unordered collection of @@ -294,7 +294,7 @@ func demo_structs(): = yes table := {alice: "first", bob: "second"} - >> table:get(alice)! + >> table[alice]! = "first" diff --git a/examples/tomodeps/tomodeps.tm b/examples/tomodeps/tomodeps.tm index fbe297ec..1f42850b 100644 --- a/examples/tomodeps/tomodeps.tm +++ b/examples/tomodeps/tomodeps.tm @@ -28,7 +28,7 @@ func _get_file_dependencies(file:Path -> {Dependency}): func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency:{Dependency}}): return if dependencies:has(dep) - dependencies:set(dep, {:Dependency}) # Placeholder + dependencies[dep] = {:Dependency} # Placeholder dep_deps := when dep is File(path): _get_file_dependencies(path) @@ -50,7 +50,7 @@ func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency:{Dependen module_deps:add(file_dep) module_deps[] - dependencies:set(dep, dep_deps) + dependencies[dep] = dep_deps for dep2 in dep_deps: _build_dependency_graph(dep2, dependencies) @@ -80,7 +80,7 @@ func _draw_tree(dep:Dependency, dependencies:{Dependency:{Dependency}}, already_ child_prefix := prefix ++ (if is_last: " " else: "│ ") - children := dependencies:get(dep) or {:Dependency} + children := dependencies[dep] or {:Dependency} for i,child in children.items: is_child_last := (i == children.length) _draw_tree(child, dependencies, already_printed, child_prefix, is_child_last) @@ -89,7 +89,7 @@ func draw_tree(dep:Dependency, dependencies:{Dependency:{Dependency}}): printed := @{:Dependency} say(_printable_name(dep)) printed:add(dep) - deps := dependencies:get(dep) or {:Dependency} + deps := dependencies[dep] or {:Dependency} for i,child in deps.items: is_child_last := (i == deps.length) _draw_tree(child, dependencies, already_printed=printed, is_last=is_child_last) |
