Arrays
Arrays are ordered, heterogeneous lists created with [...] literals.
let nums: int[] = [1, 2, 3, 4, 5]
let mixed = [1, "two", true, 3.14]
let empty: string[] = []
Indexing
Indexing is zero-based for reading and writing, and supports Python-style negative indices:
let first = nums[0] // 1
let last = nums[-1] // 5
nums[0] = 99
len(nums) returns the element count. Newlines inside [...] are ignored, so
literals may span multiple lines.
append
The append built-in returns a new array — reassign to keep the result:
let xs = [1, 2, 3]
xs = append(xs, 4) // [1, 2, 3, 4]
Array methods
Arrays have a rich set of methods, called with dot syntax. Higher-order methods
take an anonymous function fn(element, index).
| Method | Description |
|---|---|
push(...items) | Append items (mutates); returns new length |
pop() | Remove and return the last element |
first() / last() | First / last element |
slice(start, end) | Sub-array [start, end) |
contains(v) / indexOf(v) | Membership / first index (−1 if absent) |
join(sep) | Join into a string |
reverse() / sort() | Reverse / sort in place |
flatten() | Flatten one level of nested arrays |
map(fn) | Transform each element into a new array |
filter(fn) | Keep elements where fn is truthy |
reduce(fn, init?) | Fold to a single value |
forEach(fn) | Run fn for each element |
find(fn) / findIndex(fn) | First matching element / its index |
every(fn) / some(fn) | All / any element passes fn |
let nums = [1, 2, 3, 4, 5, 6]
let evenSquares = nums
.filter(fn(n) { return n % 2 == 0 })
.map(fn(n) { return n * n })
let total = nums.reduce(fn(acc, n) { return acc + n }, 0)
println(evenSquares.join(", ")) // "4, 16, 36"
println(total) // 21
For other structures — maps, sets, stacks, queues, heaps, tries — see the
collections module.