This language bar is your friend. Select your favorite languages!

Idiom #165 Last element of list

Assign to variable x the last element of list items.

x = items[-1]
import std.range;
int[] items;
auto x = items.back();
int[42] items;
int x = items[$-1];
x = items.last;
x := items[len(items)-1]
foo :: [a] -> Maybe a
foo [] = Nothing
foo xs = Just $ last xs

x = foo items
var x = items[items.length - 1];
local x = items[#items]
x := items[high(items)];
my $x = $items[-1];
x = items[-1]
x = items.last
let x = items[items.len()-1];
val x = items.takeRight(1)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org