Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,4 @@ For more examples, see:

---

**Next**: Learn about [Core Stream Operations](./stream-operations.md)
**Next**: Learn about [Working with Streams](./working-with-streams.md)
2 changes: 1 addition & 1 deletion doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ New to Phunkie Streams? Start here:
- ❌ Connection Pooling
- ❌ Process Management (dedicated Process class)

See [README.md](../README.md) for the complete "What's Implemented" section.
See the [README](https://github.com/phunkie/streams#whats-implemented) for the complete "What's Implemented" section.

## Examples

Expand Down
41 changes: 13 additions & 28 deletions src/Ops/Pull/InfinitePull/ImmListOps.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,40 @@

namespace Phunkie\Streams\Ops\Pull\InfinitePull;

use function Phunkie\Streams\Functions\transformation\take;

use Phunkie\Streams\Infinite\Timer;
use Phunkie\Streams\Pull\InfinitePull;
use Phunkie\Streams\Pull\ValuesPull;

/**
* List-like operations for InfinitePull. Converts to finite ValuesPull when bounded.
* List-like operations for InfinitePull.
*
* @method \Phunkie\Streams\Infinite\Infinite getInfinite()
* @method \Phunkie\Streams\Type\Scope getScope()
* @method bool valid()
* @method mixed pull()
* @method static appendTransformation(\Phunkie\Streams\Type\Transformation $transformation)
*/
trait ImmListOps
{
/**
* Take the first $n elements from the infinite source.
* Returns InfinitePull for Timer sources (preserving timing), or ValuesPull otherwise.
* Take the first $n elements from the infinite source, pulling no further once they are taken.
* A Timer source becomes a bounded timer so its timing is preserved.
*
* @param int $n Number of elements to take
* @return InfinitePull|ValuesPull
* @return InfinitePull
*/
public function take(int $n): InfinitePull | ValuesPull
public function take(int $n): InfinitePull
{
$infinite = $this->getInfinite();

if ($infinite instanceof Timer) {
$smallerInfinite = new InfinitePull(
awakeEvery($infinite->getSeconds(), $n)
);
$smallerInfinite->setScope($this->getScope());
$bounded = new InfinitePull(awakeEvery($infinite->getSeconds(), $n));
$bounded->setScope($this->getScope());

return $smallerInfinite;
return $bounded;
}

$values = [];

for ($i = 0; $i < $n; $i++) {
if ($this->valid()) {
$values[] = $this->pull();
}
}

$infinite->reset();

$valuesPull = new ValuesPull(
...$values
);
$valuesPull->setScope($this->getScope());
$this->appendTransformation(take($n));

return $valuesPull;
return $this;
}
}
31 changes: 31 additions & 0 deletions tests/Feature/Streams/InfiniteTakeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use function Phunkie\Effect\Functions\io\io;

describe("take on an infinite source", function () {

it("takes the first n values", function () {
expect(Stream(fromRange(1))->take(5)->compile->toArray())->toBe([1, 2, 3, 4, 5]);
});

it("composes with the operations that follow it", function () {
$list = Stream(iterate(1)(fn ($x) => $x * 2))->take(3)->map(fn ($x) => $x + 1)->compile->toList();

expect($list->toArray())->toBe([2, 3, 5]);
});

it("pulls one value at a time instead of materialising the n values first", function () {
$count = 0;
$counting = function () use (&$count) {
return io(function () use (&$count) {
$count++;
});
};
$before = memory_get_peak_usage();

Stream(fromRange(1))->take(300000)->evalTap($counting)->compile()->drain()->unsafeRun();

expect($count)->toBe(300000);
expect(memory_get_peak_usage() - $before)->toBeLessThan(8 * 1024 * 1024);
});
});
Loading