repeat

Description

The repeat function decorator will repeatedly apply a function a given number of times.

Synopsis

template<class Integral>
constexpr auto repeat(Integral);

Semantics

assert(repeat(std::integral_constant<int, 0>{})(f)(xs...) == f(xs...));
assert(repeat(std::integral_constant<int, 1>{})(f)(xs...) == f(f(xs...)));
assert(repeat(0)(f)(xs...) == f(xs...));
assert(repeat(1)(f)(xs...) == f(f(xs...)));

Requirements

Integral must be:

  • Integral

Or:

  • IntegralConstant

Example

#include <boost/hof.hpp>
#include <cassert>

struct increment
{
    template<class T>
    constexpr T operator()(T x) const
    {
        return x + 1;
    }
};

int main() {
    auto increment_by_5 = boost::hof::repeat(std::integral_constant<int, 5>())(increment());
    assert(increment_by_5(1) == 6);
}