Core typesCollections
Collections
list, iter, map and set: growable and keyed containers, and the lazy sequences that walk them.
| Type | What it holds | Page |
|---|---|---|
list<T>, written [T] | A growable sequence, indexed from 0. | list |
[T; N] | A fixed-size sequence stored inline; the size is part of the type. | array |
xs[a..b] | A temporary window onto part of a list, array or text. | slices |
iter<T> | A lazy sequence: the result of map, filter, split, chars and the like. | iter |
map<K, V> | Keys with values, in insertion order. | map |
set<T> | Unique values, in insertion order. | set |
Other containers have their own pages: braids (a fixed-size run with alternating types) and, in the guide, boxes.
Rules every collection shares
- Value semantics. Assigning a collection moves it.
copy xsmakes an independent second value. Passing one to a function that changes it needsinout. - Changing methods end in
!.xs.push!(1)changesxs, soxsmust be avar. Methods that return a new collection leave the receiver alone (sorted,reversed). - Out-of-range access stops the program.
xs[9]isstopped: index out of range,m["missing"]iskey not found. The method formsgetreturn an option instead. - Equality is by value.
[1, 2] == [1, 2]istrue. Map and set equality ignore order. - Printing works for any collection whose elements print:
[1, 2],{"a": 1},{1, 2}, and{:}for an empty map.