Skip to content

Commit

Permalink
refactor: Unpack operation - Use UnpackIterableAggregate.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 4, 2022
1 parent 741cbce commit aaa289a
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/Operation/Unpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Closure;
use Generator;
use Iterator;
use loophp\iterators\IterableIterator;
use loophp\iterators\IterableIteratorAggregate;
use loophp\iterators\UnpackIterableAggregate;
use Traversable;

/**
* @immutable
Expand All @@ -22,6 +24,8 @@
*
* @template TKey
* @template T of array{0: NewTKey, 1: NewT}
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Unpack extends AbstractOperation
{
Expand All @@ -32,31 +36,12 @@ final class Unpack extends AbstractOperation
*/
public function __invoke(): Closure
{
$toIterableIterator = static fn (iterable $value): Iterator => new IterableIterator($value);

$callbackForKeys =
/**
* @param NewTKey $initial
* @param T $value
*
* @return NewTKey
*/
static fn (int $key, array $value) => $value[0];

$callbackForValues =
/**
* @param T $value
*
* @return NewT
*/
static fn (array $value) => $value[1];

/** @var Closure(Iterator<TKey, T>): Generator<NewTKey, NewT> $pipe */
$pipe = Pipe::of()(
Map::of()($toIterableIterator),
Map::of()(static fn (iterable $iterable): Iterator => (new IterableIteratorAggregate($iterable))->getIterator()),
Map::of()(Chunk::of()(2)),
Flatten::of()(1),
Associate::of()($callbackForKeys)($callbackForValues)
static fn (Iterator $iterator): Traversable => (new UnpackIterableAggregate($iterator))->getIterator()
);

// Point free style.
Expand Down

0 comments on commit aaa289a

Please sign in to comment.