diff --git a/src/Operation/Reduction.php b/src/Operation/Reduction.php index 5f26d8f30..6ffc68467 100644 --- a/src/Operation/Reduction.php +++ b/src/Operation/Reduction.php @@ -11,6 +11,7 @@ use Closure; use Generator; +use loophp\iterators\ReductionIterableAggregate; /** * @immutable @@ -48,13 +49,6 @@ public function __invoke(): Closure * * @return Generator */ - static function (iterable $iterable) use ($callback, $initial): Generator { - foreach ($iterable as $key => $value) { - /** @var W $initial */ - $initial = $callback($initial, $value, $key, $iterable); - - yield $key => $initial; - } - }; + static fn (iterable $iterable): Generator => yield from new ReductionIterableAggregate($iterable, Closure::fromCallable($callback), $initial); } }