Skip to content

Commit 06deec5

Browse files
authored
feat: better name mappedSetsFromIterable (#29)
- deprecated mappedValueSetsFromIterable
1 parent 6b70ca7 commit 06deec5

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/ds.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ function mapFromIterable(iterable $iterable, callable $mapper): Map
5454
}
5555

5656
/**
57+
* @deprecated Use {@link mappedSetsFromIterable} instead
58+
*
5759
* @param iterable<K, V> $iterable
5860
* @param callable(K, V): Pair<KReturn, VReturn> $mapper
5961
*
@@ -84,6 +86,37 @@ function mappedValueSetsFromIterable(iterable $iterable, callable $mapper): Map
8486
return $map;
8587
}
8688

89+
/**
90+
* @param iterable<K, V> $iterable
91+
* @param callable(K, V): Pair<KReturn, VReturn> $mapper
92+
*
93+
* @return Map<KReturn, Set<VReturn>>
94+
*
95+
* @template K
96+
* @template V
97+
* @template KReturn
98+
* @template VReturn
99+
*/
100+
function mappedSetsFromIterable(iterable $iterable, callable $mapper): Map
101+
{
102+
/** @var Map<KReturn, Set<VReturn>> $map */
103+
$map = new Map();
104+
105+
foreach ($iterable as $key => $value) {
106+
$keyValue = $mapper($key, $value);
107+
$set = $map->get($keyValue->key, null);
108+
if ($set === null) {
109+
/** @var Set<VReturn> $set */
110+
$set = new Set();
111+
$map->put($keyValue->key, $set);
112+
}
113+
114+
$set->add($keyValue->value);
115+
}
116+
117+
return $map;
118+
}
119+
87120
/**
88121
* @param iterable<K, V> $iterable
89122
* @param callable(K,V): VReturn $mapper

tests/DsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
use function Cdn77\Functions\mapFromEntries;
1313
use function Cdn77\Functions\mapFromIterable;
14-
use function Cdn77\Functions\mappedValueSetsFromIterable;
14+
use function Cdn77\Functions\mappedSetsFromIterable;
1515
use function Cdn77\Functions\setFromIterable;
1616
use function Cdn77\Functions\vectorFromIterable;
1717

1818
#[CoversFunction('Cdn77\Functions\mapFromIterable')]
19-
#[CoversFunction('Cdn77\Functions\mappedValueSetsFromIterable')]
19+
#[CoversFunction('Cdn77\Functions\mappedSetsFromIterable')]
2020
#[CoversFunction('Cdn77\Functions\setFromIterable')]
2121
#[CoversFunction('Cdn77\Functions\vectorFromIterable')]
2222
final class DsTest extends TestCase
@@ -53,7 +53,7 @@ public function testMapFromIterable(): void
5353
self::assertFalse($map->get(4));
5454
}
5555

56-
public function testMappedValueSetsFromIterable(): void
56+
public function testMappedSetsFromIterable(): void
5757
{
5858
/** @var callable():Generator<int, string> $iterableFactory */
5959
$iterableFactory = static function (): Generator {
@@ -63,7 +63,7 @@ public function testMappedValueSetsFromIterable(): void
6363
yield 2 => 'b';
6464
};
6565

66-
$map = mappedValueSetsFromIterable(
66+
$map = mappedSetsFromIterable(
6767
$iterableFactory(),
6868
static fn (int $key, string $value) => new Pair($key * 2, $value . '_')
6969
);

0 commit comments

Comments
 (0)