Arrays\takeUntil()

higher-order transformer short-circuit accepts iterable returns Closure pure
(T → bool) → (Iterable → Iterable)
At a glance — Lazy prefix — walk forward, stop as soon as the predicate is true. Safe with infinite Generators.

Creates a Closure that yields elements of an array or iterable until the predicate returns true — stops exclusive (the triggering element is NOT included).

/**
  * @param callable(mixed): bool $conditional
  * @return Closure(iterable<int|string, mixed>):(array<int|string, mixed>|\Generator<int|string, mixed>)
  */
Arrays\takeUntil(callable $conditional): Closure

Returned Closure

When Arrays\takeUntil() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return array<int|string, mixed>|\Generator<int|string, mixed>
  */
$function (iterable $source): array|\Generator

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$untilNegative = Arrays\takeUntil(fn($n) => $n < 0);
print_r($untilNegative([1, 2, 3, -1, 4])); // [1, 2, 3]

Details

Arrays Functions

Releated Array take Functions