mirror of
https://github.com/boostorg/hof.git
synced 2026-01-31 20:22:11 +00:00
76 lines
1.7 KiB
C++
76 lines
1.7 KiB
C++
/*=============================================================================
|
|
Copyright (c) 2014 Paul Fultz II
|
|
tap.h
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
==============================================================================*/
|
|
|
|
#ifndef FIT_GUARD_FUNCTION_TAP_H
|
|
#define FIT_GUARD_FUNCTION_TAP_H
|
|
|
|
/// tap
|
|
/// ===
|
|
///
|
|
/// Description
|
|
/// -----------
|
|
///
|
|
/// The `tap` function invokes a function on the first argument passed in and
|
|
/// then returns the first argument. This is useful in a chain of pipable
|
|
/// function to perform operations on intermediate results. As a result, this
|
|
/// function is [`pipable`](pipable.md).
|
|
///
|
|
/// Synopsis
|
|
/// --------
|
|
///
|
|
/// template<class T, class F>
|
|
/// pipable constexpr T tap(T&& x, const F& f);
|
|
///
|
|
/// Requirements
|
|
/// ------------
|
|
///
|
|
/// F must be:
|
|
///
|
|
/// * [UnaryCallable](concepts.md#unarycallable)
|
|
///
|
|
/// Example
|
|
/// -------
|
|
///
|
|
/// struct sum_f
|
|
/// {
|
|
/// template<class T, class U>
|
|
/// T operator()(T x, U y) const
|
|
/// {
|
|
/// return x+y;
|
|
/// }
|
|
/// };
|
|
///
|
|
/// const pipable_adaptor<sum_f> sum = {};
|
|
/// // Prints 3
|
|
/// int r = 1 | sum(2) | tap([](int i) { std::cout << i; }) | sum(2);
|
|
/// assert(r == 5);
|
|
///
|
|
|
|
#include <fit/pipable.hpp>
|
|
#include <fit/apply.hpp>
|
|
#include <fit/detail/static_const_var.hpp>
|
|
|
|
namespace fit { namespace detail {
|
|
|
|
struct tap_f
|
|
{
|
|
template<class T, class F>
|
|
constexpr T operator()(T&& x, const F& f) const
|
|
{
|
|
return fit::apply(f, x), FIT_FORWARD(T)(x);
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
FIT_DECLARE_STATIC_VAR(tap, pipable_adaptor<detail::tap_f>);
|
|
|
|
|
|
} // namespace fit
|
|
|
|
#endif
|