tcbrindle / flux

A C++20 library for sequence-orientated programming
https://tristanbrindle.com/flux/
Boost Software License 1.0
476 stars 29 forks source link

take().filter() sometimes skips last element #62

Closed tcbrindle closed 1 year ago

tcbrindle commented 1 year ago

Given

auto seq = flux::ints(1).take(10).filter(flux::pred::even);

then the internal iteration code path returns five elements as expected:

seq.for_each([](int i) { 
    std::cout << i << ' ';
});
// prints 2 4 6 8 10

However external iteration misses out the last element:

FLUX_FOR(int i, seq) {
    std::cout << i << ' ';
}
// prints 2 4 6 8

https://godbolt.org/z/8ozEsj77d

tcbrindle commented 1 year ago

Even more obviously wrong:

FLUX_FOR(int i, ints(0).take(5)) { std::cout << i << ' ';  }

prints the expected 0 1 2 3 4, but

auto true_ = [](int) { return true; };

FLUX_FOR(int i, ints(0).take(5).filter(true_)) { std::cout << i << ' '; }

prints just 0 1 2 3

https://godbolt.org/z/YMnfvhPfd