Array.length
func Array.length[T](xs: Array[T]): intArray.length returns the number of elements in xs.
Details
- Returns the length at the time of the call; Array.push, Array.pop, and the other mutating functions change it.
len(xs)returns the same count.
Basic examples
Array.length([10, 20, 30]) // 3
Array.length(["ant"]) // 1Call it through . on any array:
let colors = ["red", "green", "blue"]
colors.length() // 3Scope
An empty array has length 0. Annotate it so that T is known:
let empty: Array[string] = []
empty.length() // 0Each element counts once, whatever it holds. A nested array is one element, and null is an element like any other:
[[1, 2], [3]].length() // 2
let slots: Array[int | null] = [null, 7, null]
slots.length() // 3An element of an Array[string] is a whole string, so the length counts strings, not characters:
let words = ["naïve", "🐜", ""]
words.length() // 3Records and union members are one element each:
type Circle = { r: float64 }
type Square = { side: float64 }
type Shape = Circle | Square
let shapes: Array[Shape] = [Circle{ r: 1.0 }, Square{ side: 2.0 }, Circle{ r: 0.5 }]
shapes.length() // 3In a pipeline, the array is the first argument:
"the quick brown fox" | String.split(" ") | Array.length() // 4Array.length is a function value. Pass it to Array.map to get the length of each inner array:
[[1, 2], [3], []].map(Array.length) // [2, 1, 0]The length follows the array as it grows and shrinks:
let stack = [1, 2]
stack.push(3)
stack.length() // 3
stack.pop() // 3
stack.length() // 2Applications
Read the last element by index:
let queue = ["a", "b", "c"]
queue[queue.length() - 1] // "c"Number the lines of a list with their indices:
let tasks = ["write", "test", "ship"]
for i in Array.range(0, tasks.length()) {
println("${i + 1}. ${tasks[i]}")
}1. write
2. test
3. shipCompute an average, guarding the empty case:
func average(xs: Array[float64]): float64 | null {
if xs.length() == 0 {
return null
}
var total = 0.0
for x in xs {
total += x
}
total / float64(xs.length())
}
println(average([2.0, 4.0, 9.0]))
println(average([]))5.0
nullReport a count with the right plural:
func summary(files: Array[string]): string {
let n = files.length()
if n == 1 { "1 file" } else { "${n} files" }
}
println(summary(["a.ant"]))
println(summary(["a.ant", "b.ant"]))1 file
2 filesProperties and relations
Array.length, len, and Array.isEmpty agree:
for xs in [[1, 2, 3], [], [0]] {
assert(xs.length() == len(xs))
assert(xs.isEmpty() == (xs.length() == 0))
}Array.push adds one to the length and Array.pop on a non-empty array takes one away. Array.concat adds the lengths:
let xs = [1, 2, 3]
let ys = [4, 5]
let n = xs.length()
xs.push(9)
assert(xs.length() == n + 1)
xs.pop()
assert(xs.length() == n)
assert(xs.concat(ys).length() == xs.length() + ys.length())The functions that keep every element keep the length, and a filter and its complement split it:
let xs = [5, 3, 8, 1]
assert(xs.map({ x * 2 }).length() == xs.length())
assert(xs.sort().length() == xs.length())
assert(xs.reverse().length() == xs.length())
let even = xs.filter({ x % 2 == 0 })
let odd = xs.filter({ x % 2 != 0 })
assert(even.length() + odd.length() == xs.length())
assert(even.length() == xs.countWhere({ x % 2 == 0 }))Array.range(0, n) and Array.filled(n, v) have length n:
for n in [0, 1, 5] {
assert(Array.range(0, n).length() == n)
assert(Array.filled(n, "x").length() == n)
}Possible issues
The last valid index is the length minus one. Indexing at the length panics:
let xs = [1, 2, 3]
xs[xs.length()] // panic: index 3 out of range for an array of length 3length is a function, not a field. Without parentheses it is a compile error:
let xs = [1, 2, 3]
println(xs.length) // error: Array[int] is not a record, so it has no field lengthArray.length takes only arrays. A map, a set, or a string is a compile error; use Map.size, Set.size, String.length, or len, which takes them all:
let ages = Map{ ada: 36 }
println(Array.length(ages)) // error: cannot use Map[string, int] where Array[T] is expectedA nullable array has no length until it is narrowed:
let xs: Array[int] | null = null
println(xs.length())
// error: Array[int] | null has no field or function length on every member: narrow the value firstNarrow it, or use ?. with a default:
let xs: Array[int] | null = null
xs?.length() ?? 0 // 0An array is a reference value: assigning or passing one shares it. A push through one name changes the length seen through the other:
let a = [1, 2]
let b = a
b.push(3)
a.length() // 3A for x in xs loop reads the length before each step, not once at the start, so elements pushed during the loop are visited. An unconditional xs.push(x) in the loop body never ends:
let xs = [1, 2]
for x in xs {
if x < 3 {
xs.push(x + 1)
}
}
xs // [1, 2, 2, 3, 3]To fix the count when the loop starts, read the length into a variable first, or loop over Array.range(0, xs.length()):
let xs = [1, 2]
for i in Array.range(0, xs.length()) {
xs.push(xs[i] + 1)
}
xs // [1, 2, 2, 3]See also
Array.isEmpty · Array.countWhere · Array.range · len · String.length · Bytes.length · Map.size · Set.size · Channel.length · Indexing
