Skip to content

Commit b61ae7b

Browse files
authored
Add mappedVectorsFromIterable helper (#82)
1 parent ac5d2c0 commit b61ae7b

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

src/ds.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,37 @@ function mappedSetsFromIterable(iterable $iterable, callable $mapper): Map
148148
return $map;
149149
}
150150

151+
/**
152+
* @param iterable<K, V> $iterable
153+
* @param callable(K, V): Pair<KReturn, VReturn> $mapper
154+
*
155+
* @return Map<KReturn, Vector<VReturn>>
156+
*
157+
* @template K
158+
* @template V
159+
* @template KReturn
160+
* @template VReturn
161+
*/
162+
function mappedVectorsFromIterable(iterable $iterable, callable $mapper): Map
163+
{
164+
/** @var Map<KReturn, Vector<VReturn>> $map */
165+
$map = new Map();
166+
167+
foreach ($iterable as $key => $value) {
168+
$keyValue = $mapper($key, $value);
169+
$vector = $map->get($keyValue->key, null);
170+
if ($vector === null) {
171+
/** @var Vector<VReturn> $vector */
172+
$vector = new Vector();
173+
$map->put($keyValue->key, $vector);
174+
}
175+
176+
$vector->push($keyValue->value);
177+
}
178+
179+
return $map;
180+
}
181+
151182
/**
152183
* @param iterable<K, V> $iterable
153184
* @param callable(K,V): VReturn $mapper

tests/DsTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use function Cdn77\Functions\mappedMapsFromIterable;
1515
use function Cdn77\Functions\mappedQueuesFromIterable;
1616
use function Cdn77\Functions\mappedSetsFromIterable;
17+
use function Cdn77\Functions\mappedVectorsFromIterable;
1718
use function Cdn77\Functions\setFromIterable;
1819
use function Cdn77\Functions\vectorFromIterable;
1920

2021
#[CoversFunction('Cdn77\Functions\mapFromIterable')]
2122
#[CoversFunction('Cdn77\Functions\mappedMapsFromIterable')]
2223
#[CoversFunction('Cdn77\Functions\mappedQueuesFromIterable')]
2324
#[CoversFunction('Cdn77\Functions\mappedSetsFromIterable')]
25+
#[CoversFunction('Cdn77\Functions\mappedVectorsFromIterable')]
2426
#[CoversFunction('Cdn77\Functions\setFromIterable')]
2527
#[CoversFunction('Cdn77\Functions\vectorFromIterable')]
2628
final class DsTest extends TestCase
@@ -93,7 +95,12 @@ public function testMappedQueuesFromIterable(): void
9395

9496
$map = mappedQueuesFromIterable(
9597
$iterableFactory(),
96-
static fn (int $key, string $value) => new Pair($key * 2, $value . '_'),
98+
static function (int $key, string $value): Pair {
99+
/** @phpstan-var non-falsy-string $mappedValue */
100+
$mappedValue = $value . '_';
101+
102+
return new Pair($key * 2, $mappedValue);
103+
},
97104
);
98105

99106
self::assertCount(2, $map);
@@ -118,14 +125,43 @@ public function testMappedSetsFromIterable(): void
118125

119126
$map = mappedSetsFromIterable(
120127
$iterableFactory(),
121-
static fn (int $key, string $value) => new Pair($key * 2, $value . '_'),
128+
static function (int $key, string $value): Pair {
129+
/** @phpstan-var non-falsy-string $mappedValue */
130+
$mappedValue = $value . '_';
131+
132+
return new Pair($key * 2, $mappedValue);
133+
},
122134
);
123135

124136
self::assertCount(2, $map);
125137
self::assertTrue($map->get(2)->contains('a_', 'b_'));
126138
self::assertTrue($map->get(4)->contains('a_', 'b_'));
127139
}
128140

141+
public function testMappedVectorsFromIterable(): void
142+
{
143+
$iterableFactory = static function (): Generator {
144+
yield 1 => 'a';
145+
yield 1 => 'b';
146+
yield 2 => 'c';
147+
yield 2 => 'd';
148+
};
149+
150+
$map = mappedVectorsFromIterable(
151+
$iterableFactory(),
152+
static function (int $key, string $value): Pair {
153+
/** @phpstan-var non-falsy-string $mappedValue */
154+
$mappedValue = $value . '_';
155+
156+
return new Pair($key * 2, $mappedValue);
157+
},
158+
);
159+
160+
self::assertCount(2, $map);
161+
self::assertSame(['a_', 'b_'], $map->get(2)->toArray());
162+
self::assertSame(['c_', 'd_'], $map->get(4)->toArray());
163+
}
164+
129165
public function testSetFromIterable(): void
130166
{
131167
$iterableFactory = static function (): Generator {

0 commit comments

Comments
 (0)