nikic/iter

Function slice() losing value after reach maximum index

bronek89 opened this issue · 1 comments

Hi,

I think that working of function slice is little broken right now.

Current implementation of slice function breaks iteration after fetching item from iterator that is outside of maximum index. It causes lost of last iteration element.

Consider example below:
https://3v4l.org/6B9QS

test 1 is original function
test 2 is changed function to break immediately after yielding last element

When you have a function like that:

$i = function () {
    for ($idx = 0; $idx < 1000; $idx ++) {
        echo "preprocess {$idx} \n";
        yield $idx;
    }
};

and do slice($i(), 0, 3);

Do you expect to "preprocess 3" will be executed or not?

I made PR to illustrate change that fixes described behaviuor: #76

Regards

Implemented in 956ff19 :)