HH\Lib\C\last
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;
Time complexity: O(1) if $traversable
is a Container
, O(n) otherwise.
Space complexity: O(1)
Parameters
Traversable<T>
$traversable
Returns
?T
Examples
$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