Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict\take() not playing nice with generators #216

Open
veewee opened this issue Jun 30, 2021 · 1 comment
Open

Dict\take() not playing nice with generators #216

veewee opened this issue Jun 30, 2021 · 1 comment
Assignees
Labels
Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@veewee
Copy link
Collaborator

veewee commented Jun 30, 2021

Describe the bug

When calling Dict\take() and Dict\take_while() twice on an iterator, you receive a fatal exception.

More info, see #215.

To Reproduce

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;
};
@veewee veewee added the Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Jun 30, 2021
@azjezz
Copy link
Owner

azjezz commented Oct 16, 2021

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.

@azjezz azjezz added this to the 1.9.0 milestone Oct 16, 2021
@azjezz azjezz modified the milestones: 1.9.0, 2.0.0 Oct 29, 2021
@azjezz azjezz added this to To do in v2.0.0 Nov 4, 2021
@azjezz azjezz modified the milestones: 2.0.0, 2.1.0 May 7, 2022
@azjezz azjezz removed this from To do in v2.0.0 May 7, 2022
@azjezz azjezz modified the milestones: 2.1.0, 3.0.0 Dec 19, 2022
@veewee veewee removed this from the 2.4.0 milestone May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
Development

No branches or pull requests

2 participants