Skip to content

Commit

Permalink
Update Unpack operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 8, 2020
1 parent 78a20a3 commit c253472
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Operation/Unpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,44 @@
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class Unpack extends AbstractOperation
{
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<int, array{0:TKey, 1:T}|T> $iterator
* @psalm-param Iterator<int, array{0:TKey, 1:T}> $iterator
*
* @psalm-return Generator<T, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $value) {
if (!is_iterable($value)) {
continue;
}
$isIterable =
/**
* @psalm-param T $value
*
* @param mixed $value
*/
static function ($value): bool {
return is_iterable($value);
};

$toIterableIterator = static function (iterable $value): IterableIterator {
return new IterableIterator($value);
};

/** @psalm-var callable(Iterator<TKey, T|iterable<TKey, T>>): Generator<TKey, iterable<TKey, T>> $filter */
$filter = Filter::of()($isIterable);

/** @psalm-var callable(Iterator<TKey, iterable<TKey, T>>): Generator<TKey, Iterator<TKey, T>> $map */
$map = Map::of()($toIterableIterator);

/** @psalm-var array<int, array<TKey, T>> $chunks */
$chunks = Chunk::of()(2)(new IterableIterator($value));
/** @psalm-var IterableIterator<int, array{0:TKey, 1:T}> $value */
foreach (Compose::of()($filter, $map)($iterator) as $value) {
/** @psalm-var array<int, array<T, T>> $chunks */
$chunks = Chunk::of()(2)($value);

foreach ($chunks as [$k, $v]) {
yield $k => $v;
Expand Down

0 comments on commit c253472

Please sign in to comment.