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): ClosureWhen 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|\GeneratorThis 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]