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

Add compress adaptor #84

Closed jnytra closed 1 year ago

jnytra commented 1 year ago

In this issue, I suggest adding compress sequence adaptor.

Compress adaptor filters elements from the input sequence, returning only those that have a corresponding element in selectors that evaluates to true. Stops when either the input sequence or selectors sequences has been exhausted. Roughly equivalent to:

flux::generator compress(seq, selectors) {
    // compress("ABCDEF", {1,0,1,0,1,1}) --> A C E F
    for (auto [v, s]: flux::zip(seq, selectors)) {
        if (s) {
            co_yield v;
        }
    }
}
tcbrindle commented 1 year ago

This would definitely be a useful adaptor 👍

I'm not sure about the name "compress" though... is there precedent for this name in other languages/libraries?

jnytra commented 1 year ago

I did small research:

tcbrindle commented 1 year ago

Thanks for doing the research 🙂. The Python version seems like what we want I think.

I'm still not 100% convinced about the name but I definitely like the idea of the adaptor.

I'm on vacation at the moment but I'll have a look at it when I get back unless you fancy working on it!

jnytra commented 1 year ago

It's fine, there's no need to rush. Enjoy your holiday.

tcbrindle commented 1 year ago

I've got an implementation in #85 (which I've tentatively called select_by, because I don't like the name compress very much). Unfortunately it looks like an unrelated Github Actions update has broken the CI, so I'll have to fix that first.

tcbrindle commented 1 year ago

Finally merged as flux::mask(), woo!