Skip to content

Commit

Permalink
refactor: Simplify and optimize Transpose.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Feb 6, 2022
1 parent aacf7df commit 38cca46
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/Operation/Transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

use Closure;
use Generator;
use loophp\iterators\IterableIteratorAggregate;
use loophp\iterators\MultipleIterableAggregate;
use MultipleIterator;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Transpose extends AbstractOperation
{
/**
* @psalm-suppress ImpureMethodCall - using MultipleIterator as an internal tool which is not returned
*
* @return Closure(iterable<TKey, T>): Generator<TKey, list<T>>
*/
public function __invoke(): Closure
Expand All @@ -47,14 +47,7 @@ public function __invoke(): Closure

/** @var Closure(iterable<TKey, T>): Generator<TKey, list<T>> $pipe */
$pipe = (new Pipe())()(
(new Reduce())()(
static function (MultipleIterator $acc, iterable $iterable): MultipleIterator {
$acc->attachIterator((new IterableIteratorAggregate($iterable))->getIterator());

return $acc;
}
)(new MultipleIterator(MultipleIterator::MIT_NEED_ANY)),
(new Flatten())()(1),
static fn (iterable $iterables): MultipleIterableAggregate => new MultipleIterableAggregate($iterables, MultipleIterator::MIT_NEED_ANY),
(new Associate())()($callbackForKeys)($callbackForValues)
);

Expand Down

0 comments on commit 38cca46

Please sign in to comment.