use Psl\Dict;
$gen = (function() {
$i = 0;
while (true) {
yield ++$i;
}
})();
// Take example:
$list1 = Dict\take($gen, 3);
$list2 = Dict\take($gen, 3);
// Take while example:
$list1 = Dict\take_while($gen, fn ($i) => $i <= 3);
$list2 = Dict\take_while($gen, fn ($i) => $i <= 6);
Result:
Fatal error: Uncaught Exception: Cannot rewind a generator that was already run in src/Psl/Dict/slice.php on line 42
Exception: Cannot rewind a generator that was already run in src/Psl/Dict/slice.php on line 42
or
Result:
Fatal error: Uncaught Exception: Cannot rewind a generator that was already run in src/Psl/Dict/take_while.php on line 29
Exception: Cannot rewind a generator that was already run in src/Psl/Dict/take_while.php on line 29
Expected behavior
It should list:
[1...3]
[3...6]
Additional context
Handling take like this does the trick:
function take (Generator $stream, int $n) {
$res = [];
for ($i = 0; $i < $n; $i++) {
if (!$stream->valid()) {
break;
}
$res[] = $stream->current();
$stream->next();
}
return $res;
};
fixing this just for take/drop functions might be a good idea, but we have to evaluate the performance impact, thankfully, we now have benchmarks, so we can spot massive performance drops easily.
Describe the bug
When calling
Dict\take()
andDict\take_while()
twice on an iterator, you receive a fatal exception.More info, see https://github.com/azjezz/psl/pull/215.
To Reproduce
Result:
or
Result:
Expected behavior It should list:
Additional context
Handling take like this does the trick: