Reference · Lists

min

signature
min(list | null) -> any

What 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)))
output
10.75apple25

Edge cases

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]))
output
null null 0B 102 2.05 1.0

Errors

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.

this one fails
print(min([1, "a"]))
output
line 1: `min` cannot compare str with int
this one fails
print(min([dec("1"), 1.5]))
output
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)
this one fails
print(min([1, float("nan")]))
output
line 1: `min` cannot compare float with int

Chaining

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")
output
3fignothing

See also

max, sort, sort_by, first, sum, vec.argmin

Thread
7Useful? Like it, or add to its thread below.

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