Lists, Maps and Sets
Kova has three collections, each written with square brackets:
| Type | Holds | Written |
|---|---|---|
List<T> | values in order, reached by position | [12, 5, 18] |
Map<K, V> | a value for each key | ["Ana" := 31, "Bo" := 25] |
Set<T> | values, each at most once | Set["red", "blue"] |
The type between <…> says what they hold, and everything inside must be of that type. All three
own what they hold: putting a value in moves it there, and the collection
frees its elements when it goes away.
fn main { var scores: List<Int> := [12, 5, 18] scores.add(7) print(scores) print(scores.length) print(scores[0]) scores[1] := 6 print(scores.remove(0)) print(scores) print(scores.sorted) print(scores[1..3])}[12, 5, 18, 7]41212[6, 18, 7][6, 7, 18][18, 7]- Positions start at 0.
l[i]reads,l[i] := vreplaces. - A position past the end stops the program (
index out of bounds, exit code 101) instead of reading whatever memory is there. l.add(v)putsvat the end,l.remove(i)takes the element atiout and gives it.l[1..3]is a new list with copies of the elements 1 and 2;l[1..=3]includes 3.l.sortedgives a sorted copy;l.sorted { a: Int, b: Int => a > b }sorts by your own order.- An empty list is written
[], where its type is known:let empty: List<Int> := [].
Walking through a list
Section titled “Walking through a list”fn main { var prices: List<Int> := [10, 25, 40] for p in prices { print(p) } for p in var prices { p := p * 2 } print(prices) var total: Int := 0 for p in move prices { total += p } print(total)}102540[20, 50, 80]150for p in pricesreads each element; the list cannot change during the loop.for p in var pricesmay change each element in place.for p in move pricestakes each element out; the list is used up afterwards.
Lists also take blocks: l.filter { x: Int => x > 10 },
l.map { x: Int => x * 2 }, l.any, l.all, l.fold, l.each. Every method is in
List.
A Map finds a value from its key, quickly, however many entries it holds:
fn main { var ages: Map<String, Int> := ["Ana" := 31, "Bo" := 25] ages["Cy"] := 40 ages["Bo"] := 26 print(ages["Bo"]) print(ages["Zed"]) print(ages.contains("Ana")) for name, age in ages { print("{name}: {age}") }}26nonetrueAna: 31Bo: 26Cy: 40m[k] := vadds the key or replaces its value.m[k]is aV?: the key may be missing, and Kova makes you say what to do then (see Missing values), instead of inventing a value or crashing.- Keys are kept in the order they were first added, so a program always walks a map the same way.
m.remove(k)takes an entry out and gives its value (noneif there was none);m.keysandm.valuesgive lists.
Counting is the classic use. Since counts[w] may be missing, a match says what to do in each
case:
fn main { let text: String := "the cat and the dog and the bird" var counts: Map<String, Int> := [] for w in text.words { match counts[w] { none => counts[w] := 1 n => counts[w] := n + 1 } } print(counts)}["the" := 3, "cat" := 1, "and" := 2, "dog" := 1, "bird" := 1]Keys may be any type that can be compared with =: numbers, texts, dates, and your own types
that follow Equal.
A Set keeps each value once, and tells quickly whether it holds one:
fn main { var seen: Set<String> := Set["red", "blue"] seen.add("red") seen.add("green") print(seen) print(seen.contains("blue")) print(seen.length)}Set["red", "blue", "green"]true3Set[…] is written with its name, so that […] always means a list or a map. Like a map, a set
keeps the order values were first added.
Comparing collections
Section titled “Comparing collections”= compares two lists element by element: same length, same elements, in the same order. A list
in Kova is a value, so that is what equal means. + makes a new list, += adds to a list:
fn main { let a: List<Int> := [1, 2] print(a = [1, 2]) print(a = [2, 1]) var b: List<Int> := a + [3] b += [4, 5] print(b)}truefalse[1, 2, 3, 4, 5]The elements must be comparable themselves. Maps and sets work the same way, with what suits them:
= ignores the order, + on two sets keeps one of each value, and + on two maps keeps the value
of the right one when both have a key.
fn main { print(Set[1, 2] = Set[2, 1]) print(Set[1, 2] + Set[2, 3]) print(["a" := 1] + ["a" := 2, "b" := 3])}trueSet[1, 2, 3]["a" := 2, "b" := 3]The blocks of a list, each, filter, map, any, all and fold, work on sets and maps too.
A map’s block takes the key and the value:
fn main { let ages: Map<String, Int> := ["Ada" := 36, "Alan" := 41] print(ages.filter { name: String, age: Int => age > 40 }) print(ages.map { name: String, age: Int => "{name}: {age}" })}["Alan" := 41]["Ada: 36", "Alan: 41"]