compress

Description

The compress function adaptor uses a binary function to apply a fold operation to the arguments passed to the function. Additionally, an optional initial state can be provided, otherwise the first argument is used as the initial state.

The arguments to the binary function, take first the state and then the argument.

Synopsis

template<class F, class State>
constexpr compress_adaptor<F, State> compress(F f, State s);

template<class F>
constexpr compress_adaptor<F> compress(F f);

Semantics

assert(compress(f, z)() == z);
assert(compress(f, z)(x, xs...) == compress(f, f(z, x))(xs...));
assert(compress(f)(x) == x);
assert(compress(f)(x, y, xs...) == compress(f)(f(x, y), xs...));

Requirements

State must be:

  • CopyConstructible

F must be:

Example

#include <fit.hpp>
#include <cassert>

struct max_f
{
    template<class T, class U>
    constexpr T operator()(T x, U y) const
    {
        return x > y ? x : y;
    }
};
int main() {
    assert(fit::compress(max_f())(2, 3, 4, 5) == 5);
}

References