Skip to content

Commit c689ada

Browse files
committed
[Collection] Stream emitsPairs method.
1 parent d73e9f2 commit c689ada

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/Fp/Streams/Stream.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public static function emits(iterable $source): self
143143
return new self($source);
144144
}
145145

146+
/**
147+
* @inheritDoc
148+
* @template TKI
149+
* @template TVI
150+
* @param iterable<TKI, TVI> $source
151+
* @return self<array{TKI, TVI}>
152+
*/
153+
public static function emitsPairs(iterable $source): self
154+
{
155+
return self::emits(asGenerator(function () use ($source) {
156+
foreach ($source as $key => $val) {
157+
yield [$key, $val];
158+
}
159+
}));
160+
}
161+
146162
/**
147163
* @inheritDoc
148164
* @param 0|positive-int $seconds

src/Fp/Streams/StreamEmitter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public static function emit(mixed $elem): Stream;
4040
*/
4141
public static function emits(iterable $source): Stream;
4242

43+
/**
44+
* Emits key/value pairs from iterable source
45+
*
46+
* @template TKI
47+
* @template TVI
48+
* @param iterable<TKI, TVI> $source
49+
* @return self<array{TKI, TVI}>
50+
*/
51+
public static function emitsPairs(iterable $source): self;
52+
4353
/**
4454
* Repeat this stream an infinite number of times.
4555
*

tests/Runtime/Classes/Stream/StreamEmitterTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public function testRange(): void
3131
$this->assertEquals([0, 1], Stream::range(0, 2)->toArray());
3232
$this->assertEquals([0, 2, 4], Stream::range(0, 5, 2)->toArray());
3333
}
34+
35+
public function testEmitsPairs(): void
36+
{
37+
$this->assertEquals([['a', 1]], Stream::emitsPairs(['a' => 1])->toArray());
38+
}
3439
}

tests/Static/Classes/Stream/StreamStaticTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class StreamStaticTest
1212
* @psalm-param int $input
1313
* @psalm-return Stream<int>
1414
*/
15-
public function testEmit(mixed $input): mixed
15+
public function testEmit(mixed $input): Stream
1616
{
1717
return Stream::emit($input);
1818
}
@@ -21,8 +21,17 @@ public function testEmit(mixed $input): mixed
2121
* @psalm-param array{1, 2, 'a'} $input
2222
* @psalm-return Stream<1|2|'a'>
2323
*/
24-
public function testEmits(mixed $input): mixed
24+
public function testEmits(mixed $input): Stream
2525
{
2626
return Stream::emits($input);
2727
}
28+
29+
/**
30+
* @psalm-param array<int, string> $input
31+
* @psalm-return Stream<array{int, string}>
32+
*/
33+
public function testEmitsPairs(mixed $input): Stream
34+
{
35+
return Stream::emitsPairs($input);
36+
}
2837
}

0 commit comments

Comments
 (0)