From 102a45deaa3d2147cb6b88d33dcdf5c87284a038 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera <pol.dellaiera@protonmail.com> Date: Fri, 8 Jan 2021 20:11:56 +0100 Subject: [PATCH] refactor: Update Distinct operation. --- src/Operation/Distinct.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Operation/Distinct.php b/src/Operation/Distinct.php index e9e76cd15..cf04406fa 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -8,8 +8,6 @@ use Generator; use Iterator; -use function in_array; - /** * @psalm-template TKey * @psalm-template TKey of array-key @@ -24,33 +22,34 @@ public function __invoke(): Closure { $foldLeftCallback = /** - * @psalm-param array<int, list<array{0:TKey, 1:T}>> $seen - * @psalm-param array{0:TKey, 1:T} $value + * @psalm-param list<array{0: TKey, 1: T}> $seen * - * @psalm-return array<int, list<array{0:TKey, 1:T}>> + * @psalm-param array{0: TKey, 1: T} $value */ static function (array $seen, array $value): array { - $return = false; + $isSeen = false; + + foreach ($seen as $item) { + if ($item[1] === $value[1]) { + $isSeen = true; - foreach ($seen as $seenTuple) { - if ($seenTuple[1] === $value[1]) { - $return = true; break; } } - if (false === $return) { + if (false === $isSeen) { $seen[] = $value; } return $seen; }; + /** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */ $pipe = Pipe::of()( Pack::of(), FoldLeft::of()($foldLeftCallback)([]), Unwrap::of(), - Unpack::of(), + Unpack::of() ); // Point free style.