Skip to content

Commit

Permalink
refactor: Update Unpack operation.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
drupol committed Apr 23, 2022
1 parent cd11914 commit 34c36da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
17 changes: 5 additions & 12 deletions src/Operation/Unpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

use Closure;
use Generator;
use IteratorAggregate;
use loophp\iterators\IterableIteratorAggregate;
use loophp\iterators\UnpackIterableAggregate;

// phpcs:disable Generic.Files.LineLength.TooLong
Expand All @@ -30,15 +28,10 @@ final class Unpack extends AbstractOperation
*/
public function __invoke(): Closure
{
/** @var Closure(iterable<array-key, array{0: TKey, 1: T}>): Generator<TKey, T> $pipe */
$pipe = (new Pipe())()(
(new Map())()(static fn (iterable $iterable): iterable => new IterableIteratorAggregate($iterable)),
(new Map())()((new Chunk())()(2)),
(new Flatten())()(1),
static fn (iterable $iterable): IteratorAggregate => new UnpackIterableAggregate($iterable)
);

// Point free style.
return $pipe;
return
/**
* @param iterable<array{0: TKey, 1: T}> $iterable
*/
static fn (iterable $iterable): Generator => yield from new UnpackIterableAggregate($iterable);
}
}
39 changes: 10 additions & 29 deletions tests/unit/Traits/GenericCollectionProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -4044,42 +4044,23 @@ public function unpackOperationProvider()
$operation = 'unpack';

$input = [
['a', 'a'],
['b', 'b'],
['c', 'c'],
['d', 'd'],
['e', 'e'],
['A', 'a'],
['B', 'b'],
['C', 'c'],
['D', 'd'],
['E', 'e'],
];

yield [
$operation,
[],
$input,
[
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
'e' => 'e',
],
];

$input = [
['a', 'b', 'c' => 'c', 'd' => 'd'],
['e', 'f', 'g' => 'g', 'h' => 'h'],
['i', 'j'],
];

yield [
$operation,
[],
$input,
[
'a' => 'b',
'c' => 'd',
'e' => 'f',
'g' => 'h',
'i' => 'j',
'A' => 'a',
'B' => 'b',
'C' => 'c',
'D' => 'd',
'E' => 'e',
],
];
}
Expand Down

0 comments on commit 34c36da

Please sign in to comment.