tcbrindle / flux

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

Add flux::unpack() function #91

Closed tcbrindle closed 1 year ago

tcbrindle commented 1 year ago

unpack takes an n-ary function as an argument, and returns a function object which takes an n-tuple and calls the original function with the tuple arguments. It's basically a lazy version of std::apply, roughly equivalent to

auto unpack = [](auto fn) {
    return [fn](auto tuple) -> decltype(auto) {
        return std::apply(fn, tuple);
    };
};

It's useful when dealing with sequences whose elements are tuple-like, for example zip or cartesian_product sequences, because you can use unpack to avoid having to manually write an intermediate lambda which uses structured bindings (or std::apply) yourself, for example:

flux::cartesian_product(flux::ints(0, 10), flux::ints(0, 10))
    .for_each(flux::unpack([](auto x, auto y) {
         // do something with x and y coords
    });