HH\Lib\C\firstx

Meta Engineer?

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

Returns the first element of the given Traversable, or throws if the Traversable is empty

namespace HH\Lib\C;

function firstx<T>(
  Traversable<T> $traversable,
): T;
  • For possibly empty Traversables, see C\first.
  • For possibly null Traversables, see C\nfirst.
  • For single-element Traversables, see C\onlyx.
  • For Awaitables that yield Traversables, see C\firstx_async.

Time complexity: O(1) Space complexity: O(1)

Parameters

Returns

  • T

Examples

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

$empty_traversable = vec[];
$first_element = C\firstx($empty_traversable);
//Output: Hit a php exception : exception 'InvariantViolationException' with message
//'HH\Lib\C\firstx: Expected at least one element.'