Slide 20 of 37

Maps

Setting, getting and walking
proc main(): void {
	mut hive.map.Map<Str, Int> counts = hive.map.new()
	hive.map.set(counts, "bees", 12)
	hive.map.set(counts, "hives", 3)

	if hive.map.get(counts, "bees") is Result.Ok(n) {
		echo n
	}
	echo hive.map.has(counts, "wasps")
	hive.map.delete(counts, "hives")
	echo hive.map.keys(counts)
	echo hive.map.values(counts)
}
Crossing to and from a Table
proc main(): void {
	mut hive.map.Map<Str, Str> labels = hive.map.new()
	hive.map.set(labels, "bees", "many")
	echo hive.map.toTable(labels)

	echo hive.map.get(hive.map.fromTable([["wasps", "few"]]), "wasps")
}
hive.map.Map<K, T> is the one collection that is not a vector. A key is compared and hashed whole, so it must be a Str, Int, Float, Bool, Atom, or a declared type whose every field is one of those. new() says only "empty", so it has to land somewhere already typed — a mut declaration, here. set and delete need that mut map; get answers by key with a Result, has asks the same question without the value, and keys/values walk it in the order keys were set, which is what makes echoing one reproducible from run to run. fromTable and toTable cross to and from a Table of two-cell rows, for a map whose keys and values are both Str.