Fix learnxiny

This commit is contained in:
Bruce Hill 2025-02-21 14:56:48 -05:00
parent 91f66d80bb
commit c9aa9dcdcc

View File

@ -104,13 +104,13 @@ func main():
stop x stop x
# Tables are efficient hash maps # Tables are efficient hash maps
table := {"one": 1, "two": 2} table := {"one"=1, "two"=2}
>> table["two"] >> table["two"]
= 2 : Int? = 2 : Int?
# The value returned is optional because none will be returned if the key # The value returned is optional because none will be returned if the key
# is not in the table: # is not in the table:
>> table["xxx"]! >> table["xxx"]
= none : Int? = none : Int?
# Optional values can be converted to regular values using `!` (which will # Optional values can be converted to regular values using `!` (which will
@ -123,7 +123,7 @@ func main():
= 0 : Int = 0 : Int
# Empty tables require specifying the key and value types: # Empty tables require specifying the key and value types:
empty_table := {:Text:Int} empty_table := {:Text,Int}
# Tables can be iterated over either by key or key,value: # Tables can be iterated over either by key or key,value:
for key in table: for key in table:
@ -141,22 +141,22 @@ func main():
# Tables can have a fallback table that's used as a fallback when the key # Tables can have a fallback table that's used as a fallback when the key
# isn't found in the table itself: # isn't found in the table itself:
table2 := {"three": 3; fallback=table} table2 := {"three"=3; fallback=table}
>> table2["two"]! >> table2["two"]!
= 2 = 2
>> table2["three"]! >> table2["three"]!
= 3 = 3
# Tables can also be created with comprehension loops: # Tables can also be created with comprehension loops:
>> {x:10*x for x in 5} >> {x=10*x for x in 5}
= {1:10, 2:20, 3:30, 4:40, 5:50} = {1=10, 2=20, 3=30, 4=40, 5=50}
# If no default is provided and a missing key is looked up, the program # If no default is provided and a missing key is looked up, the program
# will print an error message and halt. # will print an error message and halt.
# Any types can be used in tables, for example, a table mapping arrays to # Any types can be used in tables, for example, a table mapping arrays to
# strings: # strings:
table3 := {[10, 20]: "one", [30, 40, 50]: "two"} table3 := {[10, 20]="one", [30, 40, 50]="two"}
>> table3[[10, 20]]! >> table3[[10, 20]]!
= "one" = "one"
@ -243,7 +243,7 @@ func takes_many_types(
floating_point_number:Num, floating_point_number:Num,
text_aka_string:Text, text_aka_string:Text,
array_of_ints:[Int], array_of_ints:[Int],
table_of_text_to_bools:{Text:Bool}, table_of_text_to_bools:{Text,Bool},
pointer_to_mutable_array_of_ints:@[Int], pointer_to_mutable_array_of_ints:@[Int],
optional_int:Int?, optional_int:Int?,
function_from_int_to_text:func(x:Int -> Text), function_from_int_to_text:func(x:Int -> Text),
@ -293,7 +293,7 @@ func demo_structs():
>> "$alice" == 'Person(name="Alice", age=30)' >> "$alice" == 'Person(name="Alice", age=30)'
= yes = yes
table := {alice: "first", bob: "second"} table := {alice="first", bob="second"}
>> table[alice]! >> table[alice]!
= "first" = "first"
@ -338,8 +338,8 @@ func demo_enums():
>> "$my_shape" == "Shape.Circle(1)" >> "$my_shape" == "Shape.Circle(1)"
= yes = yes
>> {my_shape:"nice"} >> {my_shape="nice"}
= {Shape.Circle(1):"nice"} = {Shape.Circle(1)="nice"}
func demo_lambdas(): func demo_lambdas():
# Lambdas, or anonymous functions, can be used like this: # Lambdas, or anonymous functions, can be used like this: