diff --git a/src/Operation/Distinct.php b/src/Operation/Distinct.php index e4d36c4b7..e9e76cd15 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -22,27 +22,38 @@ final class Distinct extends AbstractOperation */ public function __invoke(): Closure { - /** @psalm-var list $seen */ - $seen = []; - - $filterCallback = + $foldLeftCallback = /** - * @param mixed $value - * @psalm-param T $value + * @psalm-param array> $seen + * @psalm-param array{0:TKey, 1:T} $value + * + * @psalm-return array> */ - static function ($value) use (&$seen): bool { - $return = in_array($value, $seen, true); + static function (array $seen, array $value): array { + $return = false; + + foreach ($seen as $seenTuple) { + if ($seenTuple[1] === $value[1]) { + $return = true; + break; + } + } - /** @psalm-var list $seen */ - $seen[] = $value; + if (false === $return) { + $seen[] = $value; + } - return !$return; + return $seen; }; - /** @psalm-var Closure(Iterator): Generator $filter */ - $filter = Filter::of()($filterCallback); + $pipe = Pipe::of()( + Pack::of(), + FoldLeft::of()($foldLeftCallback)([]), + Unwrap::of(), + Unpack::of(), + ); // Point free style. - return $filter; + return $pipe; } }