Array.min
func Array.min[T: Ordered](xs: Array[T]): T | nullArray.min returns the least element of xs, or null when xs is empty. It returns NaN when any element is NaN, and holds -0.0 below 0.0.
Details
- Returns
nullwhenxsis empty, so give it a default with??before using it as aT. - Returns
NaNwhen any element isNaN, and holds-0.0less than0.0. - Leaves
xsunchanged.
Basic examples
Array.min([3, 1, 2]) // 1
Array.min(["pear", "apple", "fig"]) // "apple"Call it through . on any array:
let temps = [12.5, -3.0, 7.25]
temps.min() // -3.0An empty array gives null. Annotate it so that T is known:
let empty: Array[int] = []
Array.min(empty) // nullScope
Every numeric type works, the sized ones included:
let levels: Array[byte] = [byte(200), byte(7), byte(64)]
levels.min() // 7
[-5, 3].min() // -5
[Math.Infinity, 2.0].min() // 2.0Strings compare by code point, so every upper-case ASCII letter comes before every lower-case one, and a precomposed accented letter such as é (U+00E9) comes after z:
["b", "B", "a"].min() // "B"
["é", "z", "e"].min() // "e"
["zé", "zebra"].min() // "zebra"A decomposed é is an e followed by a combining accent, so it starts with U+0065 and comes before z:
["z", "e\u{301}"].min() == "e\u{301}" // trueA NaN anywhere makes the result NaN, wherever it is:
[1.5, Math.NaN, -2.0].min() // NaN
[Math.NaN, 1.0].min() // NaN-0.0 counts as less than 0.0, in either order:
[0.0, -0.0].min() // -0.0
[-0.0, 0.0].min() // -0.0The values of a map or a set are an array, so Array.min gives the least of them:
let ages = Map{ ann: 31, bob: 27, cy: 45 }
ages.values().min() // 27
Set.of([4, 2, 9]).values().min() // 2In a pipe, the array is the first argument:
"5 12 3 8" | String.split(" ") | Array.map({ x.toInt() ?? 0 }) | Array.min() // 3Applications
Find the length of the shortest word:
["kiwi", "fig", "banana"].map(String.length).min() // 3ISO 8601 dates sort as strings, so the least string is the earliest date:
["2026-03-01", "2025-12-31", "2026-01-15"].min() // "2025-12-31"Report the lowest score, or say there is none:
func lowest(scores: Array[int]): string {
let low = scores.min()
if low == null {
return "no scores yet"
}
"lowest: ${low}, ${100 - low} below full marks"
}
println(lowest([88, 72, 95]))
println(lowest([]))lowest: 72, 28 below full marks
no scores yetShift every value so that the least becomes zero:
let readings = [14, 9, 21, 12]
let base = readings.min() ?? 0
readings.map({ x - base }) // [5, 0, 12, 3]Properties and relations
The result is an element of the array, and no element is below it:
for xs in [[5, 3, 8], [7], [2, 2, 9]] {
let low = xs.min()
assert(low != null)
assert(xs.contains(low))
assert(xs.every({ low <= x }))
}Without NaN, it agrees with the first element of Array.sort, and with Math.min folded over the array by Array.reduce:
for xs in [[4, 1, 3], [9], [6, 6, 2]] {
assert(xs.min() == xs.sort().first())
assert(xs.min() == xs.reduce(Math.maxInt, Math.min))
}The result is null exactly when the array is empty:
for xs in [["a"], []] {
assert((xs.min() == null) == xs.isEmpty())
}Array.min does not change the array:
let xs = [3, 1, 2]
assert(xs.min() == 1)
assert(xs == [3, 1, 2])Possible issues
An empty array literal gives T nothing to infer from, so it is a compile error:
println(Array.min([])) // error: cannot infer the element type of an empty array; annotate itAnnotate the array so that T is known:
let empty: Array[int] = []
Array.min(empty) // nullThe result is T | null, not T, so using it as a T is a compile error:
let xs = [3, 1]
println(xs.min() + 1) // error: + needs both operands of one type, got int | null and intGive a default with ??, or narrow a local:
let xs = [3, 1]
(xs.min() ?? 0) + 1 // 2An array of records has no order, so it is a compile error. To find the record with the least field, use Array.minBy:
type Item = { name: string, price: int }
let items = [Item{ name: "pen", price: 3 }, Item{ name: "ink", price: 2 }]
println(items.min())
// error: T would be Item, which is not a member of its bound Orderedtype Item = { name: string, price: int }
let items = [Item{ name: "pen", price: 3 }, Item{ name: "ink", price: 2 }]
items.minBy({ x.price }) // Item{ name: "ink", price: 2 }A named primitive type is not assignable to its base, so it does not fit Ordered:
type Meters = float64
let legs = [Meters(3.0), Meters(1.5)]
println(legs.min())
// error: T would be Meters, which is not a member of its bound Ordered; unwrap it with float64(...)Unwrap the elements with the base type's name first:
type Meters = float64
let legs = [Meters(3.0), Meters(1.5)]
legs.map(float64).min() // 1.5An element type that includes null is a union, which is not a single member of Ordered:
let cells: Array[int | null] = [4, null, 2]
println(cells.min())
// error: T would be int | null, which is not a single member of its bound Ordered; narrow firstMap each null to Math.maxInt to get an Array[int]:
let cells: Array[int | null] = [4, null, 2]
cells.map({ x ?? Math.maxInt }).min() // 2Math.maxInt is only a stand-in: it wins when every element is null, and the result is then Math.maxInt instead of null:
let cells: Array[int | null] = [null, null]
cells.map({ x ?? Math.maxInt }).min() // 9223372036854775807A single NaN hides every other value. Drop the NaN elements first when they mean "no reading":
let samples = [2.5, Math.NaN, 0.5]
samples.min() // NaN
samples.filter({ !Math.isNaN(x) }).min() // 0.5Array.minBy with the element itself as the key is not the same function. It keeps the first of equal keys, so it does not hold -0.0 below 0.0, and a NaN never replaces the element it holds:
[0.0, -0.0].minBy({ x }) // 0.0
[0.0, -0.0].min() // -0.0
[1.0, Math.NaN].minBy({ x }) // 1.0
[1.0, Math.NaN].min() // NaNSee also
Array.max · Array.minBy · Array.sort · Array.sum · Array.reduce · Math.min · Comparison operators · ?? operator · Generics guide
