Skip to content

Commit

Permalink
refactor: Update Distinct operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Oct 2, 2020
1 parent 7dfe75f commit 9d2579c
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@ final class Distinct extends AbstractOperation
*/
public function __invoke(): Closure
{
return
/** @psalm-var list<T> $seen */
$seen = [];

$filterCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param T $value
*
* @psalm-return Generator<TKey, T>
* @param mixed $value
*/
static function (Iterator $iterator): Generator {
$seen = [];

foreach ($iterator as $key => $value) {
if (false !== in_array($value, $seen, true)) {
continue;
}
static function ($value) use (&$seen): bool {
/** @psalm-var list<T> $seen */
$return = !in_array($value, $seen, true);

$seen[] = $value;
$seen[] = $value;

yield $key => $value;
}
return $return;
};

/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $compose */
$filter = Filter::of()($filterCallback);

return $filter;
}
}

0 comments on commit 9d2579c

Please sign in to comment.