Skip to content

Commit

Permalink
feat: better name mappedSetsFromIterable
Browse files Browse the repository at this point in the history
- deprecated mappedValueSetsFromIterable
  • Loading branch information
simPod committed Nov 14, 2023
1 parent 6b70ca7 commit 5e33cfb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/ds.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function mapFromIterable(iterable $iterable, callable $mapper): Map
}

/**
* @deprecated Use {@link mappedSetsFromIterable} instead
*
* @param iterable<K, V> $iterable
* @param callable(K, V): Pair<KReturn, VReturn> $mapper
*
Expand Down Expand Up @@ -84,6 +86,37 @@ function mappedValueSetsFromIterable(iterable $iterable, callable $mapper): Map
return $map;
}

/**
* @param iterable<K, V> $iterable
* @param callable(K, V): Pair<KReturn, VReturn> $mapper
*
* @return Map<KReturn, Set<VReturn>>
*
* @template K
* @template V
* @template KReturn
* @template VReturn
*/
function mappedSetsFromIterable(iterable $iterable, callable $mapper): Map
{
/** @var Map<KReturn, Set<VReturn>> $map */
$map = new Map();

foreach ($iterable as $key => $value) {
$keyValue = $mapper($key, $value);
$set = $map->get($keyValue->key, null);
if ($set === null) {
/** @var Set<VReturn> $set */
$set = new Set();
$map->put($keyValue->key, $set);
}

$set->add($keyValue->value);
}

return $map;
}

/**
* @param iterable<K, V> $iterable
* @param callable(K,V): VReturn $mapper
Expand Down
8 changes: 4 additions & 4 deletions tests/DsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

use function Cdn77\Functions\mapFromEntries;
use function Cdn77\Functions\mapFromIterable;
use function Cdn77\Functions\mappedValueSetsFromIterable;
use function Cdn77\Functions\mappedSetsFromIterable;
use function Cdn77\Functions\setFromIterable;
use function Cdn77\Functions\vectorFromIterable;

#[CoversFunction('Cdn77\Functions\mapFromIterable')]
#[CoversFunction('Cdn77\Functions\mappedValueSetsFromIterable')]
#[CoversFunction('Cdn77\Functions\mappedSetsFromIterable')]
#[CoversFunction('Cdn77\Functions\setFromIterable')]
#[CoversFunction('Cdn77\Functions\vectorFromIterable')]
final class DsTest extends TestCase
Expand Down Expand Up @@ -53,7 +53,7 @@ public function testMapFromIterable(): void
self::assertFalse($map->get(4));
}

public function testMappedValueSetsFromIterable(): void
public function testMappedSetsFromIterable(): void
{
/** @var callable():Generator<int, string> $iterableFactory */
$iterableFactory = static function (): Generator {
Expand All @@ -63,7 +63,7 @@ public function testMappedValueSetsFromIterable(): void
yield 2 => 'b';
};

$map = mappedValueSetsFromIterable(
$map = mappedSetsFromIterable(
$iterableFactory(),
static fn (int $key, string $value) => new Pair($key * 2, $value . '_')
);
Expand Down

0 comments on commit 5e33cfb

Please sign in to comment.