takeUntil()¶
The take operator processes a stream source and yields items until the predicate callable indicates that iteration should stop.
- class RunOpenCode\Component\Dataset\Operator\TakeUntil¶
- __construct(iterable<TKey, TValue> $source, callable(TValue, TKey=): bool $predicate)¶
- Parameters:
$source –
iterable<TKey, TValue>Stream source to iterate over.$predicate –
callable(TValue, TKey=): boolPredicate callable to evaluate stop condition.
- getIterator()¶
- Returns:
\Traversable<TKey, TValue>First N items from the stream source until predicate callable was satisfied.
Use cases¶
When only the first N items need to be yielded until some condition is met.
Example¶
1<?php
2
3new Stream(['a' => 1, 'b' => 2, 'c' => 3])
4 ->takeUntil(static fn(int $value, string $key): bool => $value > 2);
5 // yields 'a' => 1, 'b' => 2