Reference · Lists
max
max(list | null) -> anyWhat it does
The largest item of a list, or null when the list is empty. Numbers compare as
numbers (ints, floats and decimals together), strings by code point. It takes one list,
not several values: max([a, b]), not max(a, b).
Examples
print(max([3, 1, 2]))print(max([1, dec("2.75"), 2]))print(max(["pear", "apple", "fig"]))let people = [{ name: "Ann", age: 30 }, { name: "Bo", age: 25 }]print(max(map(people, (p) => p.age)))32.75pear30Edge cases
max([])andmax(null)arenull, not an error. Give a default with??.- Strings compare by code point, so a lower-case letter is larger than any capital
and
"9"is larger than"10". - Equal items: the first one wins, so
max([2, 2.0])is2andmax([2.0, 2])is2.0. - A one-item list gives that item.
- A dec with an int compares exactly; a dec with a float does not compare at all (see Errors).
print(max([]), max(null), max([]) ?? 0)print(max(["b", "B", "a"]), max(["10", "9"]))print(max([2, 2.0]), max([2.0, 2]))print(max([5]), max([dec("1.0"), 1]))null null 0b 92 2.05 1.0Errors
An argument that is not a list or null is a runtime error, not an error value. Two
items that do not order are a runtime error too: a string with a number, null,
booleans, lists, maps, NaN, or a decimal with a float (convert one side first), in
the browser as on the server.
print(max([null, 1]))line 1: `max` cannot compare int with nullprint(max([true, false]))line 1: `max` cannot compare bool with boolprint(max([dec("1"), 1.5]))line 1: `max` cannot compare float with dec: a dec is exact and a float is not, so convert one with dec(x) or float(x)Chaining
In a pipeline max takes the list and needs no arguments: xs | map(f) | max. For
the item with the largest key rather than the key itself, sort by it and take the last.
let words = ["pear", "fig", "apple"]print(words | map(len) | max)print(words | sort_by(len) | last)print([] | max ?? 0)5apple0See also
min, sort, sort_by, last, sum, vec.argmax
Thread 0 posts
On max(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo