tomo/test/tables.tm

91 lines
1.3 KiB
Plaintext
Raw Normal View History

2024-04-28 11:58:55 -07:00
func main():
>> t := {"one":1, "two":2}
= {"one":1, "two":2}
2024-04-12 10:09:31 -07:00
2024-11-30 12:50:54 -08:00
>> t["one"]
= 1 : Int?
2024-11-30 12:50:54 -08:00
>> t["two"]
= 2 : Int?
2024-11-30 12:50:54 -08:00
>> t["???"]
2024-12-07 13:04:25 -08:00
= none : Int?
2024-11-30 12:50:54 -08:00
>> t["one"]!
2024-04-12 10:09:31 -07:00
= 1
2024-11-30 12:50:54 -08:00
>> t["???"] or -1
= -1
2024-04-12 10:09:31 -07:00
t_str := ""
2024-04-28 11:58:55 -07:00
for k,v in t:
t_str ++= "($k:$v)"
2024-04-12 10:09:31 -07:00
>> t_str
= "(one:1)(two:2)"
2024-04-12 10:09:31 -07:00
>> t.length
2024-04-12 10:09:31 -07:00
= 2
>> t.fallback
2024-12-07 13:04:25 -08:00
= none : {Text:Int}?
2024-04-12 10:09:31 -07:00
>> t.keys
= ["one", "two"]
>> t.values
= [1, 2]
>> t2 := {"three":3; fallback=t}
= {"three":3; fallback={"one":1, "two":2}}
2024-04-12 10:09:31 -07:00
2024-11-30 12:50:54 -08:00
>> t2["one"]
= 1 : Int?
2024-11-30 12:50:54 -08:00
>> t2["three"]
= 3 : Int?
2024-11-30 12:50:54 -08:00
>> t2["???"]
2024-12-07 13:04:25 -08:00
= none : Int?
2024-04-12 10:09:31 -07:00
>> t2.length
2024-04-12 10:09:31 -07:00
= 1
>> t2.fallback
= {"one":1, "two":2} : {Text:Int}?
2024-04-12 10:09:31 -07:00
t2_str := ""
2024-04-28 11:58:55 -07:00
for k,v in t2:
t2_str ++= "($k:$v)"
2024-04-12 10:09:31 -07:00
>> t2_str
= "(three:3)"
>> {i:10*i for i in 5}
= {1:10, 2:20, 3:30, 4:40, 5:50}
>> {i:10*i for i in 5 if i mod 2 != 0}
= {1:10, 3:30, 5:50}
>> {x:10*x for x in y if x > 1 for y in [3, 4, 5] if y < 5}
= {2:20, 3:30, 4:40}
>> t3 := @{1:10, 2:20, 3:30}
>> t3:remove(3)
>> t3
= @{1:10, 2:20}
2024-08-10 12:15:38 -07:00
do:
>> plain := {1:10, 2:20, 3:30}
2024-11-30 12:50:54 -08:00
>> plain[2]!
= 20
2024-11-30 12:50:54 -08:00
>> plain[2]!
2024-08-10 12:15:38 -07:00
= 20
2024-11-30 12:50:54 -08:00
>> plain[456] or -999
2024-08-10 12:15:38 -07:00
= -999
>> plain:has(2)
= yes
>> plain:has(456)
= no
>> fallback := {4:40; fallback=plain}
>> fallback:has(1)
= yes
2024-11-30 12:50:54 -08:00
>> fallback[1] or -999
2024-08-10 12:15:38 -07:00
= 10
2024-11-30 12:50:54 -08:00
do:
>> t4 := &{"one": 1}
2024-11-30 12:50:54 -08:00
>> t4["one"] = 999
>> t4["two"] = 222
>> t4
= &{"one":999, "two":222}
2024-11-30 12:50:54 -08:00