Skip to content

Commit

Permalink
refactor: Optimize DropWhile operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 2, 2021
1 parent aa418fe commit d91eb93
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static fn (callable ...$callbacks): Closure => static function (Iterator $iterator) use ($callbacks): Generator {
for (; $iterator->valid(); $iterator->next()) {
$reduced = array_reduce(
$callbacks,
static fn (bool $carry, callable $callback): bool => ($callback($iterator->current(), $iterator->key(), $iterator)) ?
$carry :
false,
true
);

if (true === $reduced) {
continue;
}
$break = false;

break;
}
foreach ($iterator as $key => $current) {
if (false === $break) {
$reduced = array_reduce(
$callbacks,
static fn (bool $carry, callable $callback): bool => ($callback($current, $key, $iterator)) ?
$carry :
false,
true
);

if (true === $reduced) {
continue;
}

$break = true;
}

for (; $iterator->valid(); $iterator->next()) {
yield $iterator->key() => $iterator->current();
yield $key => $current;
}
};
}
Expand Down

0 comments on commit d91eb93

Please sign in to comment.