Arrays\takeWhile()

higher-order transformer short-circuit accepts iterable returns Closure pure
(T → bool) → (Iterable → Iterable)
At a glance — Lazy prefix — the complement of takeUntil. Safe with infinite Generators.

Creates a Closure that yields elements of an array or iterable while the predicate returns true — stops exclusive at the first falsy element.

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

Returned Closure

When Arrays\takeWhile() 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.

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

Details

Arrays Functions

Releated Array take Functions