Reference · Lists
min
min(list | null) -> anyWhat it does
The smallest 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: min([a, b]), not min(a, b).
Examples
print(min([3, 1, 2]))print(min([2.5, 1, dec("0.75")]))print(min(["pear", "apple", "fig"]))let people = [{ name: "Ann", age: 30 }, { name: "Bo", age: 25 }]print(min(map(people, (p) => p.age)))10.75apple25Edge cases
min([])andmin(null)arenull, not an error. Give a default with??.- Strings compare by code point, so capitals come before lower case and
"10"is less than"9". - Equal items: the first one wins, so
min([2, 2.0])is2andmin([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(min([]), min(null), min([]) ?? 0)print(min(["b", "B", "a"]), min(["10", "9"]))print(min([2, 2.0]), min([2.0, 2]))print(min([5]), min([dec("1.0"), 1]))null null 0B 102 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(min([1, "a"]))line 1: `min` cannot compare str with intprint(min([dec("1"), 1.5]))line 1: `min` cannot compare float with dec: a dec is exact and a float is not, so convert one with dec(x) or float(x)print(min([1, float("nan")]))line 1: `min` cannot compare float with intChaining
In a pipeline min takes the list and needs no arguments: xs | map(f) | min. For
the item with the smallest key rather than the key itself, sort by it and take the
first.
let words = ["pear", "fig", "apple"]print(words | map(len) | min)print(words | sort_by(len) | first)print([] | min ?? "nothing")3fignothingSee also
max, sort, sort_by, first, sum, vec.argmin
Thread 0 posts
On min(): 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