Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
197 lines
4.0 KiB
Markdown
197 lines
4.0 KiB
Markdown
# 5. Lists and maps
|
|
|
|
## Lists: `[]T`
|
|
|
|
An ordered, growable sequence.
|
|
|
|
```dino
|
|
func main() {
|
|
var nums = [10, 20, 30]
|
|
nums.push(40)
|
|
nums[0] = 11
|
|
print(nums, nums.len(), nums[1])
|
|
print(nums.pop(), nums)
|
|
print(nums[1..3]) // a copy of part of the list
|
|
|
|
var names: []string // empty list: write the type
|
|
names.push("ann")
|
|
print(names)
|
|
|
|
var total = 0
|
|
for n in nums { total += n }
|
|
print(total)
|
|
}
|
|
```
|
|
```text
|
|
[11, 20, 30, 40] 4 20
|
|
40 [11, 20, 30]
|
|
[20, 30]
|
|
[ann]
|
|
61
|
|
```
|
|
|
|
Facts:
|
|
- An empty list needs a type: `var xs: []int`.
|
|
- Going outside the list (`nums[9]`) or popping an empty list stops the program with a message.
|
|
- **Lists are values.** `var b = a` copies the list. Changing `b` leaves `a` alone.
|
|
|
|
```dino
|
|
func main() {
|
|
var a = [1, 2, 3]
|
|
var b = a
|
|
b.push(4)
|
|
print(a, b)
|
|
}
|
|
```
|
|
```text
|
|
[1, 2, 3] [1, 2, 3, 4]
|
|
```
|
|
|
|
## Maps: `map[K, V]`
|
|
|
|
A map finds a value by a **key** instead of a position, like a phone book.
|
|
|
|
```dino
|
|
func main() {
|
|
var ages = {"alice": 30, "bob": 25}
|
|
ages["carol"] = 41 // add a new entry
|
|
ages["bob"] = 26 // change an existing one
|
|
print(ages["bob"], ages.len())
|
|
print(ages)
|
|
}
|
|
```
|
|
```text
|
|
26 3
|
|
{alice: 30, bob: 26, carol: 41}
|
|
```
|
|
|
|
### Making a map
|
|
|
|
```dino
|
|
func main() {
|
|
var a = {"x": 1, "y": 2} // types are guessed from the entries
|
|
var b: map[string, int] = {} // empty: the type comes from the variable
|
|
var c = map[int, string]{} // empty: the type is written on the right
|
|
b["z"] = 26
|
|
c[7] = "seven"
|
|
print(a, b, c)
|
|
}
|
|
```
|
|
```text
|
|
{x: 1, y: 2} {z: 26} {7: seven}
|
|
```
|
|
|
|
An empty `{}` always needs a type from one of those two places.
|
|
|
|
### Keys
|
|
|
|
A key can be an `int` (or another whole-number type), a `string` or a `bool`.
|
|
The value can be anything: numbers, strings, lists, structs, even other maps.
|
|
|
|
### Reading
|
|
|
|
| Code | Result |
|
|
|------|--------|
|
|
| `m[k]` | the value; **stops the program** if `k` is missing |
|
|
| `m.has(k)` | `true` or `false` |
|
|
| `m.get(k)` | the value or `nil` (type `V?`, see [Errors and nil](07-errors-and-nil.md)) |
|
|
| `m.get_or(k, d)` | the value, or `d` if `k` is missing |
|
|
| `m.len()` | number of entries |
|
|
| `m.keys()` | a list of the keys |
|
|
| `m.values()` | a list of the values |
|
|
|
|
```dino
|
|
func main() {
|
|
var stock = {"apple": 3, "pear": 0}
|
|
print(stock.has("apple"), stock.has("fig"))
|
|
print(stock.get("apple"), stock.get("fig"))
|
|
print(stock.get_or("fig", -1))
|
|
print(stock.keys(), stock.values())
|
|
}
|
|
```
|
|
```text
|
|
true false
|
|
3 nil
|
|
-1
|
|
[apple, pear] [3, 0]
|
|
```
|
|
|
|
### Changing
|
|
|
|
`m[k] = v` adds or replaces. `m.remove(k)` deletes (and does nothing if the key is missing).
|
|
|
|
```dino
|
|
func main() {
|
|
var m = {"a": 1, "b": 2}
|
|
m["c"] = 3
|
|
m.remove("a")
|
|
print(m)
|
|
}
|
|
```
|
|
```text
|
|
{b: 2, c: 3}
|
|
```
|
|
|
|
### Counting: `+=` starts from zero
|
|
|
|
The most common map job is counting. `m[k] += 1` on a key that isn't there yet starts from
|
|
zero, so you don't have to check first.
|
|
|
|
```dino
|
|
func main() {
|
|
var counts: map[string, int] = {}
|
|
for w in "a b a c b a".split(" ") {
|
|
counts[w] += 1
|
|
}
|
|
print(counts)
|
|
}
|
|
```
|
|
```text
|
|
{a: 3, b: 2, c: 1}
|
|
```
|
|
|
|
(Only the compound forms `+= -= *= /= %=` do this. Plain *reading* a missing key with `m[k]` still stops the program.)
|
|
|
|
### Looping
|
|
|
|
Entries come out **in the order they were added**, so output is always predictable.
|
|
|
|
```dino
|
|
func main() {
|
|
var prices = {"tea": 2.5, "cake": 4.0}
|
|
for name, price in prices {
|
|
print(name, "costs", price)
|
|
}
|
|
for name in prices { // just the keys
|
|
print(name)
|
|
}
|
|
}
|
|
```
|
|
```text
|
|
tea costs 2.5
|
|
cake costs 4.0
|
|
tea
|
|
cake
|
|
```
|
|
|
|
### Maps are values too
|
|
|
|
`var b = a` copies the whole map. A map inside a struct is copied with the struct.
|
|
|
|
```dino
|
|
func main() {
|
|
var a = {"x": 1}
|
|
var b = a
|
|
b["y"] = 2
|
|
print(a.len(), b.len())
|
|
}
|
|
```
|
|
```text
|
|
1 2
|
|
```
|
|
|
|
Looking things up is fast: it does not slow down as the map grows. (`remove` has to
|
|
re-arrange the entries to keep their order, so it is slower on huge maps.)
|
|
|
|
Next: [Structs and enums](06-structs-and-enums.md)
|