Skip to content

Commit c3b87db

Browse files
authored
Make peek() return Pipeline for fluent API (#261)
1 parent 6af3b76 commit c3b87db

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ All entry points always return an instance of the pipeline.
136136
| `tap()` | Performs side effects on each element without changing the values in the pipeline. | |
137137
| `skipWhile()` | Skips elements while the predicate returns true, and keeps everything after the predicate return false just once. | |
138138
| `slice()` | Extracts a slice from the inputs. Keys are not discarded intentionally. Supports negative values for both arguments. | `array_slice` |
139-
| `peek()` | Returns the first N items as an iterable. Use `prepend()` to restore items if needed. | `array_splice` |
139+
| `peek()` | Returns the first N items as a pipeline/iterable. Use `prepend()` to restore items if needed. | `array_splice` |
140140
| `fold()` | Reduces input values to a single value. Defaults to summation. Requires an initial value. | `array_reduce`, `Aggregate`, `Sum` |
141141
| `reduce()` | Alias to `fold()` with a reversed order of arguments. | `array_reduce` |
142142
| `values()` | Keep values only. | `array_values` |

src/Standard.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,14 +788,19 @@ private static function generatorFromIterable(iterable $input): Generator
788788
}
789789

790790
/**
791-
* Returns the first N items from the pipeline as an iterable, removing them from the pipeline (destructive).
791+
* Returns the first N items from the pipeline as a new pipeline, removing them from the current pipeline (destructive).
792792
* Users can call prepend() to restore items if non-destructive behavior is needed.
793793
*
794794
* @param int<0, max> $count Number of items to peek at.
795795
*
796-
* @return iterable<TKey, TValue> Iterator of peeked items with keys preserved (including duplicate keys).
796+
* @return self<TKey, TValue> Pipeline (new instance) of peeked items with keys preserved (including duplicate keys).
797797
*/
798-
public function peek(int $count): iterable
798+
public function peek(int $count): self
799+
{
800+
return take($this->peekAsIterable($count));
801+
}
802+
803+
private function peekAsIterable(int $count): iterable
799804
{
800805
// No-op: empty pipeline or zero count
801806
if ($this->empty() || $count <= 0) {

tests/PeekTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,21 @@ public function testPeekOneDefersCosts(): void
229229
$this->assertSame([1], iterator_to_array($pipeline->peek(1)));
230230
}
231231

232+
/**
233+
* Test chaining cast() on peek result
234+
*/
235+
public function testPeekReturnsFluentPipeline(): void
236+
{
237+
$pipeline = take([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
238+
239+
$peeked = $pipeline->peek(5)
240+
->cast(fn($x) => $x * 2)
241+
->toList();
242+
243+
$this->assertSame([2, 4, 6, 8, 10], $peeked);
244+
$this->assertSame([6, 7, 8, 9, 10], $pipeline->toList());
245+
}
246+
232247
private static function xrange(int $start, int $end): iterable
233248
{
234249
for ($i = $start; $i <= $end; $i++) {

0 commit comments

Comments
 (0)