Reference · Lists
last
signature
last(list | null) -> anyWhat it does
The last item of a list, or null when the list is empty. null counts as an empty
list. xs[-1] is the same thing for a list that is known to have an item.
Examples
print(last([1, 2, 3]))print(last(["ann", "bob"]))print(last(split("a/b/c.txt", "/")))output
3bobc.txtEdge cases
last([])andlast(null)arenull, not an error. Give a default with??.- A last item that is
nullgivesnulltoo; checklenwhen the difference matters. - A list of one item: its first and last are the same item.
print(last([]), last(null), last(null) ?? "empty")print(last([1, null]), len([1, null]))print(last([7]) == first([7]))output
null null emptynull 2trueErrors
An argument that is not a list or null is a runtime error, not an error value. A
string is not a list: use slice with a negative start for
its last characters.
this one fails
print(last("abc"))output
line 1: `last` expects a list as argument 1, got strChaining
In a pipeline last takes the list and needs no arguments: the largest after a sort,
the newest of a list in order of time.
print([5, 3, 9] | sort | last)print(["pear", "fig", "apple"] | sort_by(len) | last)print("a.tar.gz" | split(".") | last)output
9applegzSee also
first, max, drop, slice, reverse
Thread
Thread 0 posts
On last(): 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