Array.maxBy
func Array.maxBy[T, K: Ordered](xs: Array[T], key: func(T): K): T | nullArray.maxBy returns the first element of xs with the greatest key, or null when xs is empty. An element replaces the one held only when its key is strictly greater, so of equal keys the first wins and a NaN key never replaces the element held.
Details
- Returns the element, not its key, and
nullwhenxsis empty. - Calls
keyonce per element, from first to last, and never stops early. - Of equal keys, the first element wins, and a
NaNkey never wins.
Basic examples
Find the longest word:
Array.maxBy(["fig", "banana", "kiwi"], { x.length() }) // "banana"An empty array has no greatest element, so the result is null:
let empty: Array[string] = []
empty.maxBy({ x.length() }) // nullFind the oldest person, and read a field of the result with ?.:
type Person = { name: string, age: int }
let people = [Person{ name: "Ann", age: 31 }, Person{ name: "Bob", age: 45 }]
people.maxBy({ x.age })?.name // "Bob"Scope
The result is the element, not its key:
[3, -7, 5].maxBy({ Math.abs(x) }) // -7Of equal keys, the first element wins:
["ab", "cd", "e"].maxBy({ x.length() }) // "ab"A string key compares by code point, so every uppercase ASCII letter comes before every lowercase one:
["apple", "Banana", "cherry"].maxBy({ x }) // "cherry"
["Zed", "apple"].maxBy({ x }) // "apple"A float key works too. To find the least, use Array.minBy:
let temps = [12.5, -3.0, 21.25]
temps.maxBy({ x }) // 21.25
temps.minBy({ x }) // -3.0A NaN key never replaces the element held. A NaN in the first element is held to the end, since no key is greater than it:
[1.0, 0.0 / 0.0, 3.0].maxBy({ x }) // 3.0
[0.0 / 0.0, 1.0, 3.0].maxBy({ x }) // NaNPass a named function instead of a lambda:
func vowels(s: string): int {
s.graphemes().countWhere({ "aeiou".contains(x) })
}
["sky", "queue", "area"].maxBy(vowels) // "queue"A key can use the local variables around it. Find the number closest to a target:
let target = 10
[3, 12, 7, 20].maxBy({ -Math.abs(x - target) }) // 12The element type may include null. Give the key a value for it with ??:
let scores: Array[int | null] = [null, 70, 85]
scores.maxBy({ x ?? 0 }) // 85Find the greatest grapheme of a string by splitting it first:
"hello".graphemes().maxBy({ x }) // "o"Call it through . or in a pipe:
["a", "bbb", "cc"].maxBy({ x.length() }) // "bbb"
["a", "bbb", "cc"] | Array.maxBy({ x.length() }) // "bbb"Applications
Find the key of the largest value in a map, through its entries:
let stock = Map{ "nut": 3, "bolt": 12, "gear": 7 }
stock.entries().maxBy({ x.value })?.key // "bolt"Find the most frequent word, with Array.groupBy:
let words = "the ant and the bee and the ant".split(" ")
words.groupBy({ x }).entries().maxBy({ x.value.length() })?.key // "the"Find the index of the greatest element, through Array.entries:
[4, 9, 2, 9].entries().maxBy({ x.value })?.index // 1Return early from a function when the array is empty, with ?? return:
type Run = { name: string, ms: float64 }
func slowest(runs: Array[Run]): string {
let run = runs.maxBy({ x.ms }) ?? return "no runs"
"${run.name} took ${run.ms} ms"
}
println(slowest([Run{ name: "parse", ms: 12.5 }, Run{ name: "check", ms: 30.0 }]))
println(slowest([]))check took 30.0 ms
no runsProperties and relations
With the identity as the key, Array.maxBy gives what Array.max gives on integers and strings:
let xs = [4, 9, 2, 9]
assert(xs.maxBy({ x }) == xs.max())
let names = ["Ann", "Bob", "Al"]
assert(names.maxBy({ x }) == names.max())Negating a float key, or a signed key that is never the most negative value of its kind, turns it into Array.minBy, and of equal keys both keep the first element. On an unsigned key, or on the most negative signed value, the negation panics; see Possible issues:
type Box = { name: string, size: int }
let boxes = [Box{ name: "a", size: 2 }, Box{ name: "b", size: 1 }, Box{ name: "c", size: 1 }]
assert(boxes.maxBy({ -x.size })?.name == "b")
assert(boxes.maxBy({ -x.size }) == boxes.minBy({ x.size }))The element it returns is the one in the array, so a change to it shows in the array:
type Item = { name: string, stock: int }
let items = [Item{ name: "nut", stock: 3 }, Item{ name: "bolt", stock: 12 }]
let item = items.maxBy({ x.stock })
if item != null {
item.stock = 0
}
assert(items[1].stock == 0)key runs once per element, from the first to the last, even after the greatest key is found:
let seen: Array[int] = []
[9, 1, 5].maxBy({ seen.push(x); x }) // 9
seen // [9, 1, 5]Possible issues
The result may be null, so reading a field of it without narrowing is a compile error:
type Person = { name: string, age: int }
let people = [Person{ name: "Ann", age: 31 }]
println(people.maxBy({ x.age }).name)
// error: Person | null has no field name: narrow the value firstUse ?., ??, or a test against null first, as in the examples above.
The key must be ordered. A bool or a record is not, which is a compile error:
println([1, 2, 3].maxBy({ x > 1 }))
// error: K would be bool, which is not a member of its bound OrderedOf equal keys, Array.maxBy keeps the first, while the last element of a stable ascending sort is the last of them:
type Run = { name: string, ms: float64 }
let runs = [Run{ name: "a", ms: 12.5 }, Run{ name: "b", ms: 9.0 }, Run{ name: "c", ms: 12.5 }]
runs.maxBy({ x.ms })?.name // "a"
runs.sortBy({ x.ms }).last()?.name // "c"Negating an unsigned key to find the least panics, since the negation overflows. So does negating the most negative value of a signed kind:
[uint8(1), uint8(2)].maxBy({ -x }) // panic: integer overflow
[Math.minInt, 0].maxBy({ -x }) // panic: integer overflowUse Array.minBy instead.
When the elements may be null, a null result does not say whether xs was empty or a null element won:
let a: Array[int | null] = [null]
a.maxBy({ x ?? 0 }) // null
let b: Array[int | null] = []
b.maxBy({ x ?? 0 }) // nullTest Array.isEmpty first, or find the index through Array.entries:
let a: Array[int | null] = [null]
a.isEmpty() // false
a.entries().maxBy({ x.value ?? 0 })?.index // 0On floats, maxBy({ x }) and Array.max can differ. Array.max is NaN when any element is NaN and puts -0.0 below 0.0, while Array.maxBy never lets a NaN key win and keeps the first of equal keys:
[1.0, 0.0 / 0.0].maxBy({ x }) // 1.0
[1.0, 0.0 / 0.0].max() // NaN
[-0.0, 0.0].maxBy({ x }) // -0.0
[-0.0, 0.0].max() // 0.0See also
Array.minBy · Array.max · Array.min · Array.sortBy · Array.find · Array.reduce · Array.entries · ?. operator · ?? operator
