code / tomo-koans

Lines447 Tomo432 INI9 Markdown6
(32 lines)
1 # Arrays
3 func main()
5 # Arrays are ordered collections of values.
6 # You can define an array using `[...]`:
8 nums := [10, 20, 30]
10 # Arrays are 1-indexed.
11 assert nums[2] == ???
13 # Arrays can be empty but must have a type:
14 empty : [Int] = []
16 assert empty == []
18 # You can loop over an array with `for value in array`:
19 sum := 0
20 for num in nums
21 sum += num
23 assert sum == ???
25 # Array comprehensions let you transform arrays concisely:
26 squares := [n*n for n in nums]
28 assert squares == [???, ???, ???]
30 # You can also get the index with `for index, value in array`:
31 for i, num in nums
32 assert squares[i] == num * num