code / tomo-koans

Lines447 Tomo432 INI9 Markdown6
(34 lines)
1 # Tables
3 func main()
5 # Tables store key-value pairs.
6 # You can define a table using `{key: value, ...}`.
8 scores := {"Alice": 100, "Bob": 200}
9 assert scores == {"Alice": 100, "Bob": 200}
11 assert scores["Alice"] == ???
13 # Accessing a missing key gives `none`
14 assert scores["Zoltan"] == ???
16 # Tables can be empty but must have key and value types:
17 empty : {Text:Int} = {}
18 assert empty == {}
20 # You can loop over tables:
21 total := 0
22 for name, score in scores
23 total += score
25 assert total == ???
27 # Table keys and values can be accessed as an array:
28 assert scores.keys == [???]
30 assert scores.values == [???]
32 # Table comprehensions let you create tables concisely:
33 doubled := {k: v * 2 for k, v in scores}
34 assert doubled == {???}