Meta Engineer?

This is available as C\last in the www repository.

Returns the last element of the given Traversable, or null if the Traversable is empty

namespace HH\Lib\C;

function last<T>(
  Traversable<T> $traversable,
): ?T;
  • For non-empty Traversables, see C\lastx.
  • For single-element Traversables, see C\onlyx.

Time complexity: O(1) if $traversable is a Container, O(n) otherwise. Space complexity: O(1)



  • ?T


$strings = vec["a", "b", "c"];
$last_string = C\last($strings);
echo "Last string in traversable: $last_string \n";
//Output: Last string in traversable: c

$empty_traversable = vec[];
$last_element = C\last($empty_traversable);
$last_element_as_string = $last_element ?? "null";
echo "Last element in empty traversable: $last_element_as_string";
//Output: Last element in empty traversable: null