-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vec counterparts for the Dict versions of these functions
- Loading branch information
1 parent
8ea808a
commit 7efaf75
Showing
8 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Vec; | ||
|
||
/** | ||
* Return all the unique values of an array, as a list. | ||
* | ||
* @template Tv | ||
* | ||
* @param iterable<Tv> $iterable | ||
* | ||
* @return list<Tv> | ||
*/ | ||
function unique(iterable $iterable): array | ||
{ | ||
return namespace\unique_by( | ||
$iterable, | ||
/** | ||
* @param Tv $v | ||
* | ||
* @return Tv | ||
* | ||
* @pure | ||
*/ | ||
static fn($v) => $v, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Vec; | ||
|
||
use Closure; | ||
use Psl\Iter; | ||
|
||
/** | ||
* Returns a new array in which each value appears exactly once, where the | ||
* value's uniqueness is determined by transforming it to a scalar via the | ||
* given function. | ||
* | ||
* @template Tv | ||
* @template Ts | ||
* | ||
* @param iterable<Tv> $iterable | ||
* @param (Closure(Tv): Ts) $scalar_func | ||
* | ||
* @return list<Tv> | ||
*/ | ||
function unique_by(iterable $iterable, Closure $scalar_func): array | ||
{ | ||
/** @var list<Ts> $unique */ | ||
$unique = []; | ||
/** @var list<Tv> $original_values */ | ||
$original_values = []; | ||
foreach ($iterable as $v) { | ||
$scalar = $scalar_func($v); | ||
|
||
if (!Iter\contains($unique, $scalar)) { | ||
$unique[] = $scalar; | ||
$original_values[] = $v; | ||
} | ||
} | ||
|
||
return $original_values; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Vec; | ||
|
||
use function array_unique; | ||
use function is_array; | ||
|
||
/** | ||
* Returns a new list in which each value appears exactly once. Better performant than `Vec\unique()` when the values | ||
* are only scalars. | ||
* | ||
* @template Tv of scalar | ||
* | ||
* @param iterable<Tv> $iterable | ||
* | ||
* @return list<Tv> | ||
*/ | ||
function unique_scalar(iterable $iterable): array | ||
{ | ||
if (is_array($iterable)) { | ||
return namespace\values(array_unique($iterable)); | ||
} | ||
|
||
return unique_by( | ||
$iterable, | ||
/** | ||
* @param scalar $v | ||
* | ||
* @return scalar | ||
* | ||
* @pure | ||
*/ | ||
static fn($v) => $v | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Vec; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Str; | ||
use Psl\Vec; | ||
|
||
final class UniqueByTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testUniqueBy(array $expected, array $array, callable $scalar_fun): void | ||
{ | ||
static::assertSame($expected, Vec\unique_by($array, $scalar_fun)); | ||
} | ||
|
||
public function provideData(): array | ||
{ | ||
return [ | ||
[ | ||
['a', 'saif'], | ||
['a', 'b', 'c', 'd', 'saif', 'jack'], | ||
static fn (string $value): int => Str\length($value), | ||
], | ||
|
||
[ | ||
['foo', 'bar', '@baz'], | ||
['foo', '@foo', 'bar', '@bar', '@baz'], | ||
static fn (string $value): string => Str\replace($value, '@', ''), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Vec; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\Iter; | ||
use Psl\Vec; | ||
|
||
final class UniqueScalarTest extends TestCase | ||
{ | ||
public function testUniqueScalars(): void | ||
{ | ||
$array = Vec\fill(10, 'foo'); | ||
$array[] = 'bar'; | ||
|
||
$unique = Vec\unique_scalar($array); | ||
|
||
static::assertCount(2, $unique); | ||
static::assertSame(['foo', 'bar'], $unique); | ||
} | ||
|
||
public function testUniqueIterator() | ||
{ | ||
$array = Iter\Iterator::create(['foo', 'foo', 'bar', 'bar', 'baz']); | ||
|
||
$unique = Vec\unique_scalar($array); | ||
|
||
static::assertCount(3, $unique); | ||
static::assertSame(['foo', 'bar', 'baz'], $unique); | ||
} | ||
|
||
public function testUniqueIteratorAgggregate() | ||
{ | ||
$array = Collection\Map::fromArray(['foo', 'foo', 'bar', 'bar', 'baz']); | ||
|
||
$unique = Vec\unique_scalar($array); | ||
|
||
static::assertCount(3, $unique); | ||
static::assertSame(['foo', 'bar', 'baz'], $unique); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Vec; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Collection; | ||
use Psl\Iter; | ||
use Psl\Vec; | ||
|
||
final class UniqueTest extends TestCase | ||
{ | ||
public function testUnique(): void | ||
{ | ||
$array = Vec\fill(10, 'foo'); | ||
|
||
$unique = Vec\unique($array); | ||
|
||
static::assertCount(1, $unique); | ||
|
||
static::assertSame('foo', Iter\first($unique)); | ||
} | ||
|
||
public function testUniqueWithObjects(): void | ||
{ | ||
$array = Vec\fill(10, 'foo'); | ||
$object = new Collection\Map([]); | ||
$array = Vec\concat($array, Vec\fill(10, $object)); | ||
|
||
$unique = Vec\unique($array); | ||
|
||
static::assertCount(2, $unique); | ||
|
||
static::assertSame('foo', Iter\first($unique)); | ||
static::assertSame($object, Iter\last($unique)); | ||
} | ||
} |