Array.max
func Array.max[T: Ordered](xs: Array[T]): T | nullArray.max returns the greatest 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.max([3, 1, 4, 1, 5]) // 5
Array.max([2.5, -1.0]) // 2.5An empty array gives null. Annotate it so that T is known:
let empty: Array[int] = []
Array.max(empty) // nullCall it through . on any array:
let temps = [18.5, 21.0, 19.25]
temps.max() // 21.0Scope
Strings work as well as numbers:
["pear", "apple", "fig"].max() // "pear"Every numeric type works, the sized ones included:
[byte(3), byte(200)].max() // 200
[float32(1.5), float32(-2.0)].max() // 1.5
[int8(-5), int8(-100)].max() // -5Infinities compare as IEEE 754 orders them:
[Math.Infinity, 1.0e300].max() // Infinity
[-Math.Infinity, -1.0].max() // -1.0A single element is the greatest:
[42].max() // 42The values of a map, and the members of a set, are arrays, so Array.max takes them:
Map{ a: 3, b: 7, c: 5 }.values().max() // 7
Set.of(["b", "c", "a"]).values().max() // "c"In a pipe, the array is the first argument:
"3 14 1 59" | String.split(" ") | Array.max() // "59"Applications
Give an empty array a default with ??:
func highScore(scores: Array[int]): int {
scores.max() ?? 0
}
println(highScore([70, 92, 85]))
println(highScore([]))92
0Find the greatest field of a list of records by mapping to the field first:
type Order = { id: string, total: float64 }
let orders = [Order{ id: "a", total: 12.5 }, Order{ id: "b", total: 40.0 }]
orders.map({ x.total }).max() // 40.0Find the latest date among ISO 8601 strings, which sort as text in date order:
["2026-03-01", "2025-12-31", "2026-11-08"].max() // "2026-11-08"Scale values to the range 0 to 1 by dividing by the greatest, narrowing a local first:
func normalize(xs: Array[float64]): Array[float64] {
let top = xs.max()
if top == null || top == 0.0 {
return xs
}
xs.map({ x / top })
}
println(normalize([1.0, 2.0, 4.0]))[0.25, 0.5, 1.0]Properties and relations
Array.max agrees with the last element of the sorted array, and with a fold through Math.max:
let xs = [3, -7, 4, 1]
assert(xs.max() == xs.sort().last())
assert(xs.max() == xs.reduce(xs[0], Math.max))The result is at least every element, and is one of them:
let xs = ["kiwi", "apple", "plum"]
let top = xs.max() ?? ""
assert(xs.every({ x <= top }))
assert(xs.contains(top))The order of the elements does not change the result, and max does not change the array:
let xs = [5, 2, 9, 2]
assert(xs.max() == xs.reverse().max())
assert(xs == [5, 2, 9, 2])Array.max is at least Array.min:
let xs = [1.5, -2.0, 0.25]
let hi = xs.max() ?? 0.0
let lo = xs.min() ?? 0.0
assert(lo <= hi)Possible issues
The result is T | null, not T, so using it as a T is a compile error:
let xs = [3, 1, 2]
println(xs.max() + 1) // error: + needs both operands of one type, got int | null and intGive a default with ??, or narrow a local:
let xs = [3, 1, 2]
(xs.max() ?? 0) + 1 // 4One NaN makes the result NaN, wherever it is. Drop it first with Math.isNaN:
let xs = [1.0, Math.NaN, 3.0]
xs.max() // NaN
xs.filter({ !Math.isNaN(x) }).max() // 3.0-0.0 and 0.0 are ==, but Array.max holds -0.0 less than 0.0, so the result does not depend on their order:
[-0.0, 0.0].max() // 0.0
[0.0, -0.0].max() // 0.0
[-0.0].max() // -0.0Mixing integer and float literals infers a union element type, int | float64, which is not a single member of Ordered, so it is a compile error:
println([1, 2.5].max())
// error: T would be int | float64, which is not a single member of its bound Ordered; narrow firstWrite every element as a float, or annotate the array:
let xs: Array[float64] = [1.0, 2.5]
[1.0, 2.5].max() // 2.5
xs.max() // 2.5An array whose element type is a union, such as int | null or Number, has no single T, so it is a compile error:
let xs: Array[int | null] = [1, null, 3]
println(xs.max())
// error: T would be int | null, which is not a single member of its bound Ordered; narrow firstCollect the non-null elements into an Array[int] first. Testing each one narrows it:
let xs: Array[int | null] = [1, null, 3]
let ns: Array[int] = []
for v in xs {
if v != null {
ns.push(v)
}
}
ns.max() // 3Because T never includes null, a null result always means an empty array.
Records, bool, and named primitive types are not Ordered:
type Meters = float64
let runs = [Meters(5.0), Meters(10.0)]
println(runs.max())
// error: T would be Meters, which is not a member of its bound Ordered; unwrap it with float64(...)Unwrap a named primitive with its base type, or pick a record by a key with Array.maxBy:
type Meters = float64
type Run = { who: string, distance: float64 }
let runs = [Meters(5.0), Meters(10.0)]
let log = [Run{ who: "Ann", distance: 5.0 }, Run{ who: "Bob", distance: 10.0 }]
runs.map({ float64(x) }).max() // 10.0
log.maxBy({ x.distance })?.who // "Bob"See also
Array.min · Array.maxBy · Array.minBy · Array.sort · Array.sum · Array.reduce · Math.max · comparison operators · ?? operator
