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 Jan 8, 2021
1 parent 0a91335 commit 102a45d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Generator;
use Iterator;

use function in_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -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.
Expand Down

0 comments on commit 102a45d

Please sign in to comment.