Skip to content

Commit 72c3336

Browse files
committed
Add Stream::init method
1 parent 681c327 commit 72c3336

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

src/Fp/Functions/Collection/Init.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Fp\Operations\InitOperation;
88

9+
use function Fp\Cast\asArray;
10+
911
/**
1012
* @template TK of array-key
1113
* @template TV
@@ -16,5 +18,5 @@
1618
*/
1719
function init(iterable $collection): array
1820
{
19-
return InitOperation::of($collection)();
21+
return asArray(InitOperation::of($collection)());
2022
}

src/Fp/Operations/InitOperation.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Fp\Operations;
66

7+
use Generator;
8+
79
/**
810
* @template TK
911
* @template TV
@@ -13,13 +15,25 @@
1315
final class InitOperation extends AbstractOperation
1416
{
1517
/**
16-
* @return array<TK, TV>
18+
* @return Generator<TK, TV>
1719
*/
18-
public function __invoke(): array
20+
public function __invoke(): Generator
1921
{
20-
$butLast = iterator_to_array($this->gen);
21-
array_pop($butLast);
22+
if (!$this->gen->valid()) {
23+
return [];
24+
}
25+
26+
while (true) {
27+
$item = $this->gen->current();
28+
$key = $this->gen->key();
29+
30+
$this->gen->next();
31+
32+
if (!$this->gen->valid()) {
33+
break;
34+
}
2235

23-
return $butLast;
36+
yield $key => $item;
37+
}
2438
}
2539
}

src/Fp/Streams/Stream.php

+10
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ public function tail(): Stream
388388
return $this->fork(Ops\TailOperation::of($this->emitter)());
389389
}
390390

391+
/**
392+
* {@inheritDoc}
393+
*
394+
* @return Stream<TV>
395+
*/
396+
public function init(): Stream
397+
{
398+
return $this->fork(Ops\InitOperation::of($this->emitter)());
399+
}
400+
391401
/**
392402
* {@inheritDoc}
393403
*

src/Fp/Streams/StreamChainableOps.php

+12
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ public function mapN(callable $callback): Stream;
203203
*/
204204
public function tail(): Stream;
205205

206+
/**
207+
* Returns every stream element except last
208+
*
209+
* ```php
210+
* >>> Stream::emits([1, 2, 3])->init()->toList();
211+
* => [1, 2]
212+
* ```
213+
*
214+
* @return Stream<TV>
215+
*/
216+
public function init(): Stream;
217+
206218
/**
207219
* Take stream elements while predicate is true
208220
*

tests/Runtime/Classes/Stream/StreamOpsTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ public function testTail(): void
217217
);
218218
}
219219

220+
public function testInit(): void
221+
{
222+
$this->assertEquals(
223+
[1, 2],
224+
Stream::emits([1, 2, 3])->init()->toList(),
225+
);
226+
}
227+
220228
public function testTakeAndDrop(): void
221229
{
222230
$this->assertEquals([0, 1], Stream::emits([0, 1, 2])->takeWhile(fn($e) => $e < 2)->toList());

0 commit comments

Comments
 (0)