Skip to content

Commit

Permalink
refactor: TakeWhile - simplify things
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Feb 15, 2023
1 parent 09eb135 commit 9d4bee7
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Generator;
use loophp\collection\Utils\CallbacksArrayReducer;
use loophp\iterators\IterableIteratorAggregate;

/**
* @immutable
Expand Down Expand Up @@ -35,20 +34,15 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (iterable $iterable) use ($callbacks): Generator {
$iteratorAggregate = (new IterableIteratorAggregate($iterable))->getIterator();
$callback = CallbacksArrayReducer::or()($callbacks);

$every = (new Every())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, mixed $value, mixed $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)($iteratorAggregate);
foreach ($iterable as $key => $current) {
if (false === $callback($current, $key, $iterable)) {
break;
}

$size = (true === $every->current()) ? -1 : $every->key();

yield from (new Limit())()($size)(0)($iteratorAggregate);
yield $key => $current;
}
};
}
}

0 comments on commit 9d4bee7

Please sign in to comment.