Skip to content

Commit

Permalink
fix: reduce operation, use ReduceIterableIterator
Browse files Browse the repository at this point in the history
Also minor performance improvements
  • Loading branch information
drupol committed Dec 28, 2022
1 parent 6f320a6 commit 884f64c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
],
"require": {
"php": ">= 8",
"loophp/iterators": "^2"
"loophp/iterators": "^2.3.1"
},
"require-dev": {
"amphp/parallel-functions": "^1",
Expand Down
18 changes: 8 additions & 10 deletions src/Operation/Reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Generator;
use loophp\iterators\ReduceIterableAggregate;

/**
* @immutable
Expand Down Expand Up @@ -35,15 +36,12 @@ public function __invoke(): Closure
*
* @return Closure(iterable<TKey, T>): Generator<TKey, W>
*/
static function (mixed $initial) use ($callback): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, W> $pipe */
$pipe = (new Pipe())()(
(new Reduction())()($callback)($initial),
(new Last())(),
);

// Point free style.
return $pipe;
};
static fn (mixed $initial): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<TKey, V>
*/
static fn (iterable $iterable): Generator => yield from new ReduceIterableAggregate($iterable, $callback, $initial);
}
}

0 comments on commit 884f64c

Please sign in to comment.