Lists, dictionaries & options
Partly builtlist<T>, string-keyed dict<T>, built-in options, list patterns and the ?? default.
Lists
list<T> is Unison’s immutable sequence. [1, 2, 3] builds one, List.map(xs, f), List.foldLeft(xs, init, f) and
List.at(xs, i), List.filter(xs, keep), List.find(xs, matches) (an option), List.reverse(xs),
List.zip(xs, ys) (tuples, up to the shorter list), List.concat(xss) and List.sum(xs) (ints) are built, and a switch can take a list apart with [], [x], [x, ...rest] and
[...init, last].
Dictionaries
dict<T> is an immutable map from string keys. It is stored as a sorted list of key/value pairs, so equal
contents hash equal whatever order they were written in. d["k"] reads and gives an option<T>; Dict.set(d, k, v)
(or d["k"] = v on a var) writes. Dict.has(d, k) and Dict.size(d) ask questions; Dict.keys(d),
Dict.values(d) and Dict.entries(d) give lists (of string, of the values, and of (string, T) tuples) in sorted
key order, and for (k, v) in d { ... } visits the entries in that same order.
Options
A built-in option<T> is what List.at and d["k"] return. It is taken apart with switch on Some(x) and
None(), or defaulted with ??.
let scores = dict{"ada": 10, "bob": 7}
let adaScore = scores["ada"] ?? 0
let carolScore = scores["carol"] ?? 0
let total = (xs: list<int>): int => List.foldLeft(xs, 0, (acc, x) => acc + x)
let describe = (xs: list<int>): string => switch xs {
| [] => "empty"
| [x] => `just ${x}`
| [x, ...rest] => `starts with ${x}`
}
let third = (xs: list<int>): int => List.at(xs, 2) ?? 0A switch is checked for exhaustiveness, including nested and list patterns.
Not built: patterns over a dict (dict{...}; membership is not a positional pattern over the sorted-list
representation), iteration over a dict, and a real map type behind dict.