Skip to content

Commit

Permalink
Update operations to use Pack instead of Wrap.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 26, 2020
1 parent 54a3e97 commit 810bff8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/Operation/Reverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public function __invoke(): Closure
/**
* @psalm-param \Iterator<TKey, T> $iterator
*
* @psalm-return \Generator<TKey|null, T, mixed, void>
* @psalm-return \Generator<TKey, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
/** @psalm-var array<TKey, T> $all */
$all = iterator_to_array((new Run(new Wrap()))($iterator));
/** @psalm-var array<int, array{0: TKey, 1: T}> $all */
$all = iterator_to_array((new Run(new Pack()))($iterator));

for (end($all); null !== key($all); prev($all)) {
$item = current($all);

yield key($item) => current($item);
yield $item[0] => $item[1];
}
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Operation/Shuffle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public function __invoke(): Closure
/**
* @psalm-param \Iterator<TKey, T> $iterator
*
* @psalm-return \Generator<TKey|null, T, mixed, void>
* @psalm-return \Generator<TKey, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
/** @psalm-var array<TKey, T> $data */
$data = iterator_to_array((new Run(new Wrap()))($iterator));
/** @psalm-var array<int, array{0: TKey, 1: T}> $data */
$data = iterator_to_array((new Run(new Pack()))($iterator));

while ([] !== $data) {
$randomKey = array_rand($data);

yield key($data[$randomKey]) => current($data[$randomKey]);
yield $data[$randomKey][0] => $data[$randomKey][1];

unset($data[$randomKey]);
}
Expand Down
45 changes: 17 additions & 28 deletions src/Operation/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,33 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param callable(T, T):(int) $callback
*
* @psalm-return \Generator<TKey, T>
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator, int $type, callable $callback): Generator {
switch ($type) {
case Operation\Sortable::BY_VALUES:
$operations = [
'before' => [new Wrap()],
'after' => [new Unwrap()],
];

break;
case Operation\Sortable::BY_KEYS:
$operations = [
'before' => [new Flip(), new Wrap()],
'after' => [new Unwrap(), new Flip()],
];

break;

default:
throw new Exception('Invalid sort type.');
if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) {
throw new Exception('Invalid sort type.');
}

$operations = Operation\Sortable::BY_VALUES === $type ?
[
'before' => [new Pack()],
'after' => [new Unpack()],
] :
[
'before' => [new Flip(), new Pack()],
'after' => [new Unpack(), new Flip()],
];

$callback =
/**
* @psalm-param array{TKey, T} $left
* @psalm-param array{TKey, T} $right
* @psalm-param array{0:TKey, 1:T} $left
* @psalm-param array{0:TKey, 1:T} $right
*/
static function (array $left, array $right) use ($callback): int {
/** @psalm-var T $left */
$left = current($left);
/** @psalm-var T $right */
$right = current($right);

return $callback($left, $right);
return $callback($left[1], $right[1]);
};

$arrayIterator = new ArrayIterator(iterator_to_array((new Run(...$operations['before']))($iterator)));
Expand Down

0 comments on commit 810bff8

Please sign in to comment.