Map and Set
Both find a key quickly, whatever their size (they use a hash table), and both keep the order in which keys were first added, so a program walks them the same way at every run.
Their keys can be any type that can be compared with =: numbers, Char, String, dates,
tuples of them, and types that follow Equal.
Map<K, V>
Section titled “Map<K, V>”| Written | Does |
|---|---|
["a" := 1, "b" := 2] | a map of these entries; [] is an empty one where the type is known |
m.length -> Int | the number of entries. |
m.is_empty -> Bool | whether the Map has no entry. |
m[k] -> V? | a copy of the value of k, none if there is none. To read a large value without copying it, use if v := m[k] { … } or match m[k] { … }: v is then lent, and m cannot change meanwhile. |
m[k] := v | gives k the value v, adding the key if needed (m must be a var). |
m[k, else v] | the value of k itself, added with the value v if it is missing; v is computed only then. It can be changed in place: groups[k, else []].add(x), counts[w, else 0] += 1 (m must be a var). |
m[k] else v | the value of k, or v if there is none, without changing m (it works on a let too): ages[name] else 0. Not to be confused with m[k, else v], which also adds the key. |
if var v := m[k] { … } | changes the value of k where it is, if there is one; m is lent meanwhile. |
m.contains(k: K) -> Bool | whether k has a value. |
m.remove(k: K) -> V? | takes the entry of k out and gives its value, none if there was none. |
m.keys -> List<K> | copies of the keys, in order. |
m.values -> List<V> | copies of the values, in the same order. |
for k, v in m { … } | walks through the entries in order. |
m.each { k: K, v: V => … } | runs the block on each entry, in order. |
m.filter { k: K, v: V => … } -> Map<K, V> | a new Map of the entries for which the block says true. |
m.map { k: K, v: V => … } -> List<U> | a List of what the block gives for each entry. |
m.any { k: K, v: V => … } -> Bool | whether the block says true for at least one entry. |
m.all { k: K, v: V => … } -> Bool | whether the block says true for every entry. |
m.fold(start) { total: U, k: K, v: V => … } -> U | goes through the entries, carrying a result. |
a = b, a != b | same keys with equal values, in any order. |
a + b -> Map<K, V> | a new Map of both; when both have a key, the value of b wins. |
a += b | adds the entries of b to a, the same way. |
m.clone -> Map<K, V> | a copy. |
fn main { var stock: Map<String, Int> := ["apple" := 12, "pear" := 4] stock["plum"] := 30 stock["pear"] := 5 print(stock["pear"]) print(stock["kiwi"]) print(stock.remove("apple")) print(stock.keys) for fruit, n in stock { print("{fruit}: {n}") }}5none12["pear", "plum"]pear: 5plum: 30m[k] is a V?, so it cannot be computed with directly: m[k] += 1 is refused, since the key may
be missing. Read it, then decide:
fn main { var counts: Map<Char, Int> := [] for c in "banana".chars { match counts[c] { none => counts[c] := 1 n => counts[c] := n + 1 } } print(counts)}['b' := 1, 'a' := 3, 'n' := 2]Counting
Section titled “Counting”A word count is the usual use of a Map. counts[w, else 0] += 1 adds the word with 0 the first time, then adds
one; sorting the keys by their count gives the most frequent first:
fn main { var counts: Map<String, Int> := [] for w in "the cat and the dog and the bird".words { counts[w, else 0] += 1 } let ranked: List<String> := counts.keys.sorted { a, b => counts[a, else 0] > counts[b, else 0] } for w in ranked[0..3] { print("{w}: {counts[w] else 0}") }}the: 3and: 2cat: 1The block’s parameters a and b are keys: the call says so, so their type is not written again.
A Map<String, T> is also read from and written to JSON objects:
JSON and CSV.
Set<T>
Section titled “Set<T>”| Written | Does |
|---|---|
Set["a", "b"] | a set of these values (a value written twice is kept once); Set[] is empty |
s.length -> Int | the number of values. |
s.is_empty -> Bool | whether the Set has no value. |
s.add(v) | adds v, unless it is already there. |
s.contains(v: T) -> Bool | whether v is there. |
s.remove(v: T) -> Bool | takes v out; true if it was there. |
for v in s { … } | walks through the values, in the order they were added. |
s.each { x: T => … } | runs the block on each value, in order. |
s.filter { x: T => … } -> Set<T> | a new Set of the values for which the block says true. |
s.map { x: T => … } -> List<U> | a List of what the block gives for each value. |
s.any { x: T => … } -> Bool | whether the block says true for at least one value. |
s.all { x: T => … } -> Bool | whether the block says true for every value. |
s.fold(start) { total: U, x: T => … } -> U | goes through the values, carrying a result. |
a = b, a != b | the same values, in any order. |
a + b -> Set<T> | a new Set of the values of both. |
a += b | adds the values of b to a. |
s.clone -> Set<T> | a copy. |
fn main { var tags: Set<String> := Set["kova", "docs"] tags.add("kova") tags.add("web") print(tags) print(tags.contains("docs")) print(tags.remove("docs")) print(tags.length)}Set["kova", "docs", "web"]truetrue2