Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better name mappedSetsFromIterable #29

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading