Skip to content

Last ​

The last element of a collection.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageLast!
Last(collection)the last element of the collection.
Last(collection, default)default when the collection is empty, instead of the last element.
Last(expr)the last operand of any expression, not just a collection.
Typed signatures
TypePackageApplies
(xs: indexed_collection<any>) -> anycompute-enginealways
(any, any?) -> anycombinatoricsreplaces compute-enginealways

Domain: Collections

Details
  • Last(c)=At(c,−1)Last(c) = At(c, -1). See At.
  • Positional indexing from the front (index 1) and back (negative indices).
  • Complements First for the other end of a collection.
  • A second argument supplies a default for an empty collection, instead of MissingMissing.
  • Works on the operands of any expression, not just a collection's elements.

Examples

3 more kept as data, checked against the oracles
Basic #
live ↗
InLast([1,2,3])\mathrm{Last}(\bigl\lbrack1, 2, 3\bigr\rbrack)Out33
live ↗
InLast([10,20,30])\mathrm{Last}(\bigl\lbrack10, 20, 30\bigr\rbrack)Out3030
live ↗
InLast([a,b,c])\mathrm{Last}(\bigl\lbrack a, b, c\bigr\rbrack)Outcc

An empty list yields the supplied default

live ↗
InLast([],x)\mathrm{Last}(\bigl\lbrack\bigr\rbrack, x)Outxx
live ↗
InLast([[a,b],[c,d],[k,f]])\mathrm{Last}(\bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack c, d\bigr\rbrack, \bigl\lbrack k, f\bigr\rbrack\bigr\rbrack)Out[k,f]\bigl\lbrack k, f\bigr\rbrack
Scope #

A second argument supplies a default for an empty collection

live ↗
InLast([],99)\mathrm{Last}(\bigl\lbrack\bigr\rbrack, 99)Out9999

Of a matrix, the last row

live ↗
InLast([[a,b],[c,d],[f,g]])\mathrm{Last}(\bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack c, d\bigr\rbrack, \bigl\lbrack f, g\bigr\rbrack\bigr\rbrack)Out[f,g]\bigl\lbrack f, g\bigr\rbrack

A lazy Range works too

live ↗
InLast(5..10)\mathrm{Last}(5..10)Out1010

Any expression, not just a list: the last term of a sum

live ↗
InLast(a2+b2)\mathrm{Last}(a^2+b^2)Outb2b^2

A default is ignored when the collection has a last element

live ↗
InLast([a,b],x)\mathrm{Last}(\bigl\lbrack a, b\bigr\rbrack, x)Outbb
live ↗
InLast(SparseArray(1..100))\mathrm{Last}(\mathrm{SparseArray}(1..100))Out100100
Properties #

Last(c)=At(c,−1)Last(c) = At(c, -1). See At

live ↗
InLast([1,2,3])=[1,2,3][−1]\mathrm{Last}(\bigl\lbrack1, 2, 3\bigr\rbrack)=\bigl\lbrack1, 2, 3\bigr\rbrack[-1]OutTrue\mathrm{True}
Possible issues #

An empty collection has no last element; compute-engine returns the symbol MissingMissing

live ↗
InLast([])\mathrm{Last}(\bigl\lbrack\bigr\rbrack)OutMissing\mathrm{Missing}
live ↗
InLast(1b)\mathrm{Last}(\frac{1}{b})Outbb
About
Examples17
Checked againstWolfram Language 15 agree1 differ1 unevaluatedmathlib4 4 agree1 error
CrosswalkWolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

The last element of a list; an empty list fails to elaborate (ours is Missing) rather than defaulting, and so does an expression that is not a list.

See also: First, At