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

Simplify element access in pipeline #193

Open
Guekka opened this issue Jul 19, 2024 · 0 comments
Open

Simplify element access in pipeline #193

Guekka opened this issue Jul 19, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Guekka
Copy link
Contributor

Guekka commented Jul 19, 2024

Description

Currently, flux::first, flux::find_if and some others return a cursor. I will call these functions collectors

This makes sense with flux iteration model, but is unwieldy in practice:

// common part
struct IndexedData
{
    int index;
    int data; // assume this is important
};

IndexedData data[] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}};
auto cur = flux::find_if(data, [](auto&& d) { return d.data == 3; });
if (flux::is_last(data, cur)) {
    return -1;
}

auto elem = flux::read_at(data, cur);
return process(elem);

What I would like instead is having an optional:

return flux::read_find_if(data, [](auto&& d) { return d.data == 3; })
        .map(process)
        .value_or(-1);

Proposed solution

I can see two ways to implement this. The tricky part is that flux::read_at needs both the sequence and the cursor, so we cannot simply do .find_if(...).read(). Adding a reference to the sequence in the cursor would be counter productive.

read_*

This solution is the most obvious. For each collector, we can provide a read_ version that will convert the cursor to an optional. We could also make the default collectors (find_if, ...) return an optional, and create cursor_* versions instead

read_self (tap?)

Another solution would be to provide a generic read_self collector. It could be used like this:

return flux::read_self(data, [](auto &&seq) { return seq.find_if([](auto &&d) { return d.data == 3; }); })
        .map(process)
        .value_or(-1)

It would mean less implementation effort, but I'm not sure it would be really ergonomic. Also, this might have side effects I did not think about

Something else?

If there is a better way, I'd love to hear about it


Sidenote: while writing this, I've noticed find_if doesn't seem tested?

@Guekka Guekka changed the title Simplify element access Simplify element access in pipeline Jul 19, 2024
@tcbrindle tcbrindle added the enhancement New feature or request label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants