Array
Array holds the functions of the built-in array type Array[T], dense and growable. xs[i] reads and xs[i] = v writes, both panicking when i is out of range; indices are ints from 0, and ranges are half-open, from inclusive and to exclusive. A function whose doc says it mutates xs changes it in place; every other returns a new array.
The Array namespace holds the functions on arrays. Each function that takes an array takes it first, so Array.map(xs, f), xs.map(f), and xs | Array.map(f) are the same call. Array.range and Array.filled build an array from scratch and are called only through the namespace.
Details
xs[i]andxs[i] = vpanic wheniis out of range; Array.get returnsnullinstead. Ranges are half-open:fromincluded,toexcluded.- A function that mutates
xs, such as Array.push, changes it in place. Every other function leaves its arguments untouched and returns a new value. - An array is a reference: copying a variable shares the array.
- Two arrays are
==when they have the same length and equal elements in order.
Basic examples
Square brackets build an array, xs[i] reads an element, and xs[i] = v writes one:
let xs = [10, 20, 30]
xs[0] // 10
len(xs) // 3
Array.get(xs, 5) // null
xs[1] = 25
xs // [10, 25, 30]Array.push grows an array in place and returns nothing:
let names: Array[string] = []
Array.push(names, "Ada")
Array.push(names, "Linus")
names // ["Ada", "Linus"]Calls chain through ., or the array goes down a pipe:
let scores = [72, 95, 88, 40]
scores.filter({ x >= 50 }).sort() // [72, 88, 95]
scores | Array.map({ x / 10 }) | Array.sum() // 28Scope
A spread copies the elements of an array into a new literal:
let xs = [1, 2]
[0, ...xs, 3] // [0, 1, 2, 3]A for loop visits the elements in order:
let steps = ["mix", "bake"]
for step in steps {
println(step)
}mix
bakeArray.entries pairs each element with its index, for a loop that needs both:
let steps = ["mix", "bake"]
for step in steps.entries() {
println("${step.index}: ${step.value}")
}0: mix
1: bakeElements may be records, null where the type allows it, or other arrays:
type Point = { x: int, y: int }
[Point{ x: 1, y: 2 }] // [Point{ x: 1, y: 2 }]
let maybe: Array[int | null] = [1, null]
maybe // [1, null]
[[1, 2], [3]].flatten() // [1, 2, 3]Functions by job
Size and access
- Array.length: the number of elements
- Array.isEmpty: whether there are none
- Array.get: element
i, ornullwhen out of range - Array.first, Array.last: the first or last element, or
null
Changing an array in place
- Array.push: append a value
- Array.extend: append the elements of another array
- Array.pop: remove and return the last element
- Array.insert: put a value at index
i - Array.removeAt: remove and return element
i
Building arrays
- Array.range: the
ints fromfromup toto - Array.filled:
ncopies of one value - Array.concat: two arrays, one after the other
- Array.slice: the elements from
fromup toto - Array.flatten: an array of arrays, concatenated
- Array.entries: each element with its index, as Indexed
Transforming
- Array.map: apply a function to each element
- Array.flatMap: map to arrays and concatenate them
- Array.filter: the elements a predicate accepts
- Array.reverse: the elements in reverse order
- Array.unique, Array.uniqueBy: the first of each equal value or key
Searching
- Array.find, Array.findLast: the first or last element a predicate accepts
- Array.findIndex, Array.findLastIndex: its index
- Array.indexOf, Array.lastIndexOf: the first or last index of a value
- Array.contains: whether a value is an element
Testing and counting
- Array.some: whether a predicate accepts any element
- Array.every: whether it accepts every element
- Array.countWhere: how many elements it accepts
Ordering
- Array.sort: ascending, stable
- Array.sortBy: ascending by a key
- Array.sortWith: by a comparison function
- Array.min, Array.max: the least or greatest element
- Array.minBy, Array.maxBy: the element with the least or greatest key
Combining and grouping
- Array.reduce: fold the elements into one value
- Array.sum: the sum of numbers, or
nullwhen empty - Array.chunk: split into arrays of
nelements - Array.groupBy: a map from each key to its elements
Identity
- Array.same: whether two arrays are the same array, not only equal
Applications
Count the words of each length. Array.groupBy keeps the keys in first-seen order:
let words = ["ant", "bee", "wasp", "fly", "moth"]
let groups = words.groupBy({ x.length() })
groups.entries().map({ "${x.key}: ${len(x.value)}" }) // ["3: 3", "4: 2"]Build a list of results in a loop, then summarize it:
let squares: Array[int] = []
for i in Array.range(1, 5) {
squares.push(i * i)
}
squares // [1, 4, 9, 16]
squares.sum() // 30
squares.max() // 16Properties and relations
len and Array.length agree, and an array is empty when its length is 0:
let xs = [4, 5, 6]
assert(len(xs) == Array.length(xs))
assert(xs.isEmpty() == (len(xs) == 0))A call through ., a pipe, and a direct call are the same call:
let xs = [3, 1, 2]
assert(xs.sort() == Array.sort(xs))
assert((xs | Array.sort()) == Array.sort(xs))Possible issues
Copying an array variable copies the reference, not the elements. A push through one name shows through the other:
let xs = [1, 2]
let ys = xs
ys.push(3)
xs // [1, 2, 3]
Array.same(xs, ys) // trueA spread into a new literal makes a separate array:
let xs = [1, 2]
let ys = [...xs]
ys.push(3)
xs // [1, 2]
xs == [1, 2] // trueOnly the functions marked mutates change the array. Array.sort returns a sorted copy and leaves xs as it was:
let xs = [3, 1, 2]
let sorted = xs.sort()
xs // [3, 1, 2]
sorted // [1, 2, 3]let fixes the binding, not the elements, so a mutating function works on a let array:
let xs = [1, 2]
xs.push(3)
xs // [1, 2, 3]Reading or writing past the end panics. Array.get returns null for an index that may be out of range:
let xs = [1, 2, 3]
xs[3] // panic: index 3 out of range for an array of length 3
xs.get(3) // nullA write past the end panics with the same message:
let xs = [1, 2, 3]
xs[3] = 1 // panic: index 3 out of range for an array of length 3Assigning the loop variable is a compile error, since it would not change the array. Array.map returns the changed elements:
let xs = [1, 2, 3]
for x in xs {
x = x * 2
// error: cannot assign to x, a for variable; declare a var to hold a value that changes
}let xs = [1, 2, 3]
xs.map({ x * 2 }) // [2, 4, 6]A for loop reads the array as it goes, so it visits an element pushed during the loop. Array.map and the other functions that take a callback read the elements once, at the call:
let a = [1, 2, 3]
let seen: Array[int] = []
for x in a {
seen.push(x)
if x < 3 {
a.push(x + 10)
}
}
seen // [1, 2, 3, 11, 12]
let b = [1, 2, 3]
b.map({ b.push(x + 10); x * 2 }) // [2, 4, 6]
b // [1, 2, 3, 11, 12, 13]See also
Map, Set, Iter, Indexed, Array type, Array literals, Indexing, len, Collections
