lazy

Description

The lazy function adaptor returns a function object call wrapper for a function. Calling this wrapper is equivalent to invoking the function. It is a simple form of lambda expressions, but is constexpr friendly. By default, lazy captures all of its variables by value, just like bind. std::ref can be used to capture references instead.

Ultimately, calling lazy(f)(x) is the equivalent to calling std::bind(f, x) except the lazy version can be called in a constexpr context, as well. The lazy adaptor is compatible with std::bind, so most of the time lazy and std::bind can be used interchangeably.

Synopsis

template<class F>
constexpr lazy_adaptor<F> lazy(F f);

Semantics

assert(lazy(f)(xs...) == std::bind(f, xs...))
assert(lazy(f)(xs...)() == f(xs...))
assert(lazy(f)(_1)(x) == f(x))
assert(lazy(f)(lazy(g)(_1))(x) == f(g(x)))

Requirements

F must be:

Example

#include <boost/hof.hpp>
#include <cassert>
using namespace boost::hof;

int main() {
    auto add = [](auto x, auto y) { return x+y; };
    auto increment = lazy(add)(_1, 1);
    assert(increment(5) == 6);
}

References