Reference · Lists
first
signature
first(list | null) -> anyWhat it does
The first item of a list, or null when the list is empty. null counts as an empty
list. xs[0] is the same thing for a list that is known to have an item.
Examples
print(first([1, 2, 3]))print(first(["ann", "bob"]))print(first(split("k=v", "=")))output
1annkEdge cases
first([])andfirst(null)arenull, not an error. Give a default with??.- A first item that is
nullgivesnulltoo; checklenwhen the difference matters. - The item comes back as it is: a list inside a list stays a list.
print(first([]), first(null), first([]) ?? 0)print(first([null, 1]), len([null, 1]) > 0)print(first([[1, 2], [3]]))output
null null 0null true[1, 2]Errors
An argument that is not a list or null is a runtime error, not an error value. A
string is not a list: use take or
slice for its first characters.
this one fails
print(first("abc"))output
line 1: `first` expects a list as argument 1, got strChaining
In a pipeline first takes the list and needs no arguments: the smallest after a
sort, the first match after a filter.
print([5, 3, 9] | sort | first)print(["pear", "fig", "apple"] | sort_by(len) | first)print([1, 2, 3] | filter((x) => x > 5) | first ?? "none")output
3fignoneSee also
Thread
Thread 0 posts
On first(): 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