Array.isEmpty
func Array.isEmpty[T](xs: Array[T]): boolArray.isEmpty reports whether xs has no elements.
Details
- Ant has no truthiness, so an array is never a condition by itself; use
Array.isEmpty(xs).
Basic examples
Array.isEmpty([1, 2, 3]) // false
let none: Array[int] = []
Array.isEmpty(none) // trueCall it through . on any array:
let queue = ["a", "b"]
queue.isEmpty() // falseScope
An array that holds only empty arrays, empty strings, or null still has elements:
[[1], []].isEmpty() // false
[""].isEmpty() // false
[null].isEmpty() // falseThe answer follows the array as it changes:
let stack = [1]
stack.isEmpty() // false
stack.pop() // 1
stack.isEmpty() // true
stack.push(2)
stack.isEmpty() // falseThe result of another function, such as Array.filter, is often what you test:
[1, 2, 3].filter({ x > 5 }).isEmpty() // trueIn a pipeline, the array is the first argument:
"a b" | String.split(" ") | Array.isEmpty() // falseAs a function value, it maps an array of arrays to an array of bools:
[[1], [], [2, 3]].map(Array.isEmpty) // [false, true, false]For an array that may be null, ?. yields null instead of calling:
let tags: Array[string] | null = null
tags?.isEmpty() // null
tags?.isEmpty() ?? true // trueNamed arguments
The parameter is named xs:
Array.isEmpty(xs: [1]) // falseApplications
Report an empty input instead of computing on it:
func average(xs: Array[float64]): float64 | null {
if xs.isEmpty() {
return null
}
var total = 0.0
for x in xs {
total += x
}
total / float64(len(xs))
}
average([1.0, 2.0, 6.0]) // 3.0
average([]) // nullWork through a queue until it runs dry:
let todo = ["b", "a"]
let done: Array[string] = []
while !todo.isEmpty() {
let item = todo.pop()
if item != null {
done.push(item)
}
}
println(done)["a", "b"]Collect validation problems and succeed only when there are none:
type User = { name: string, age: int }
func check(u: User): Result[User, Array[string]] {
let problems: Array[string] = []
if u.name.isEmpty() {
problems.push("name is empty")
}
if u.age < 0 {
problems.push("age is negative")
}
if problems.isEmpty() { Result.ok(u) } else { Result.err(problems) }
}
println(check(User{ name: "Ann", age: 31 }))
println(check(User{ name: "", age: -1 }))Ok{ value: User{ name: "Ann", age: 31 } }
Err{ error: ["name is empty", "age is negative"] }Properties and relations
Array.isEmpty agrees with len and Array.length, with == against an annotated empty array, since arrays are equal when they have the same length and == elements, and, for an array whose element type excludes null, with Array.first:
let none: Array[int] = []
let cases: Array[Array[int]] = [[], [7], [1, 2]]
for xs in cases {
assert(xs.isEmpty() == (len(xs) == 0))
assert(xs.isEmpty() == (Array.length(xs) == 0))
assert(xs.isEmpty() == (xs == none))
assert(xs.isEmpty() == (xs.first() == null))
}Possible issues
An array is not a condition. Ask Array.isEmpty explicitly:
let xs = [1]
if xs { // error: cannot use Array[int] where bool is expected
println("has elements")
}A nullable array must be narrowed or reached through ?. first:
let xs: Array[int] | null = null
println(Array.isEmpty(xs)) // error: cannot use Array[int] | null where Array[T] is expectedAn empty array literal has no element type to infer. Annotate it:
println([].isEmpty()) // error: cannot infer the element type of an empty array; annotate itAn operand of == gives no expected type, so xs == [] cannot infer the element type. Use Array.isEmpty:
let xs = [1]
println(xs == []) // error: cannot infer the element type of an empty array; annotate itArray.isEmpty takes only arrays. Strings, maps, and sets have their own:
println(Array.isEmpty("abc")) // error: cannot use string where Array[T] is expectedArray.first is null when the array is empty, but also when the first element is null. Use Array.isEmpty to test for elements:
let n: Array[int | null] = [null]
n.first() == null // true
n.isEmpty() // falseSee also
Array.length · len · Array.first · Array.pop · String.isEmpty · Map.isEmpty · Set.isEmpty
