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

views::generate doesn't work with views::chunk_by #1802

Open
gbeck75 opened this issue Nov 24, 2023 · 5 comments
Open

views::generate doesn't work with views::chunk_by #1802

gbeck75 opened this issue Nov 24, 2023 · 5 comments

Comments

@gbeck75
Copy link

gbeck75 commented Nov 24, 2023

The following code fails to compile but works as expected if I use iota() instead of generate()

// auto v = views::iota(10,20);
auto v = views::generate(
        [i=10u] () mutable {
            return i++;
        }) | views::take(10);

auto r = v | views::chunk_by(
            [](auto a, auto) {
                return a == 15;
            })

Is this to be expected?
How can I use views::chunk_by with views::generate ?

@JohelEGP
Copy link
Contributor

generate results in an input range.
iota results in a random access range.
chunk_by requires a forward range.

@gbeck75
Copy link
Author

gbeck75 commented Nov 24, 2023

Thank you!
And how can I "generate" a forward range?

@JohelEGP
Copy link
Contributor

Generating a forward range would mean having a regular invocable that returns the same value based on an index.
You can generate a forward range with iota(0) | transform(generate_function).

@gbeck75
Copy link
Author

gbeck75 commented Nov 24, 2023

Unfortunately I only have an input range.

It seems the question should really be why chunk_by needs a forward range?
In the example it should be possible to chunk the input range in a single pass, is there something like chunk_by which works on an input range?

@JohelEGP
Copy link
Contributor

I'm looking at the C++ standard library implementation.
The reason seems to be that the implementation needs two iterators into the range.
The iterator's value type is a subrange: https://eel.is/c++draft/range.chunk.by#iter.
You can probably write a version that works for input ranges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants