Maps¶
// a map is written with braces, from each key to its value
ages = {"ada": 36, "alan": 41}
print(ages["ada"], ages.len())
// assigning to a key adds its entry, or replaces the value of one it has
ages["grace"] = 85
ages["ada"] += 1
print(ages)
// in looks for a key
print("alan" in ages, "linus" in ages, "linus" not in ages)
// get gives an option, for a key that may be missing
print(ages.get("linus"), ages.get("grace").unwrap_or(0))
// for walks the keys in the order their entries were added
for name in ages:
print(name, ages[name])
for name, age in ages.items():
print("{name} is {age}")
// pop removes an entry and gives its value as an option
print(ages.pop("alan"), ages.pop("alan"))
print(ages.keys(), ages.values())
// counting is a map's classic job
counts = {}
for word in "the cat saw the dog".split():
counts[word] = counts.get(word).unwrap_or(0) + 1
print(counts)
// an entry changes in place through any place, such as a list it holds
groups = {"even": [0], "odd": []}
for i in range(1, 6):
if i % 2 == 0:
groups["even"].append(i)
else:
groups["odd"].append(i)
print(groups)
// assigning a map copies it, as with lists
copy = counts
copy["the"] = 0
print(counts["the"], copy["the"])
// a function names a map's type with its key and value types
fn total(m: map[str, int]) -> int:
sum = 0
for k in m:
sum += m[k]
ret sum
print(total(counts))
prints
36 2
{'ada': 37, 'alan': 41, 'grace': 85}
true false true
none 85
ada 37
alan 41
grace 85
ada is 37
alan is 41
grace is 85
some(41) none
['ada', 'grace'] [37, 85]
{'the': 2, 'cat': 1, 'saw': 1, 'dog': 1}
{'even': [0, 2, 4], 'odd': [1, 3, 5]}
2 0
5
Working with maps¶
A map finds a value by its key, as a Python dict does. Its type names both, as in
map[str, int], and its keys are ints, strings, or bools. {} is an empty map, which gets its
types from what is later stored in it, and {"a": 1, "b": 2} writes one with entries.
m[k]reads the value fork. A key the map has no entry for stops the program withkey not found: 'k', so usem.get(k), which gives an option, when a key may be missing.m[k] = vadds an entry fork, or replaces its value if the map has one.m[k] += 1and other changes need the entry to be there already.k in mandk not in mtest for a key, asm.contains(k)does, andm.len()counts the entries.m.pop(k)removes the entry forkand gives its value as an option,noneif there was no entry.for k in m:walks the keys, andm.keys(),m.values(), andm.items()give them, the values, and both as tuples, in lists.
A map keeps its entries in the order they were added, so walking it and printing it always give
the same order, and adding a key again after pop puts it last.
A map is a value, like a list: assigning it, passing it, or storing it makes a copy as far as
the program can tell, so a function changes its caller’s map only by returning it, as in
counts = add(counts, word), which stone turns into a change in place. An entry changes through
any place that starts at a variable, such as groups["even"].append(i) or p.counts[k] += 1.
Two maps cannot be compared with ==. See Types for the details,
and the builtins for each method.
Next, Structs.