Collections
Arrays
string[]
declares an array type.
This is an immutable ordered collection of elements.
Some common array operations are:
x ~ xs |
Prepends an element x on the left of the array xs . |
xs ~ x |
Appends an element x on the right of the array xs . |
xs ~~ ys |
Concatenates two arrays. |
xs[3] |
Accesses an element at a 0-based index. |
for x : xs; x + 1 |
Loops over the array and returns a new array. Explained in "For" and "with" expressions. |
Creating an array uses the same syntax as creating a record,
meaning it is calling a function called new
.
This function is variadic, meaning it can take any number of arguments.
Slicing
xs[i]
gets the i
th element of a array. Indices start at 0.
xs[end - j]
gets the j
th last element.
end
is one past the last element, so j
should not be 0.
xs[i .. j]
gets elements i
, i + 1
, etc.,
up to (but not including) j
.
xs[i .. end]
discards the first i
elements.
xs[0 .. end - j]
discards the last j
elements.
Mutable array
string mut[]
declares a mutable array.
Use ~=
and ~~=
to append elements or arrays.
(There is no efficient prepend; there is col/mut-dequeue for that.)
Maps
string[nat]
declares a map from
nat
s to string
s.
Mutable maps
string mut[nat]
declares a mutable map.
Shared collections
There is also t shared[]
and v shared[k]
which work like
t mut[]
and v mut[k]
but are shared
.
Other collections
There are other collections like col/set and col/mut-set in the col
directory.
There is also a col/collection module for containing functions that work on many kinds of collection.