/*============================================================================= Copyright (c) 2014 Paul Fultz II compose.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_COMPOSE_H #define FIT_GUARD_FUNCTION_COMPOSE_H /// compose /// ======= /// /// Description /// ----------- /// /// The `compose` function adaptor provides function composition. It produces /// a function object that composes a set of functions, ie the output of one /// function becomes the input of the second function. So, `compose(f, g)(0)` /// is equivalent to `f(g(0))`. /// /// /// Synopsis /// -------- /// /// template /// constexpr compose_adaptor compose(Fs... fs); /// /// Requirements /// ------------ /// /// Fs must be: /// /// FunctionObject /// MoveConstructible /// /// Example /// ------- /// /// struct increment /// { /// template /// T operator()(T x) const /// { /// return x + 1; /// } /// }; /// /// struct decrement /// { /// template /// T operator()(T x) const /// { /// return x - 1; /// } /// }; /// /// int r = compose(increment(), decrement(), increment())(3); /// assert(r == 4); /// #include #include #include #include #include #include #include #include #include namespace fit { namespace detail { template struct compose_kernel : detail::compressed_pair { typedef detail::compressed_pair base_type; FIT_INHERIT_CONSTRUCTOR(compose_kernel, base_type) FIT_RETURNS_CLASS(compose_kernel); template constexpr auto operator()(Ts&&... xs) const FIT_RETURNS ( FIT_MANGLE_CAST(const F1&)(FIT_CONST_THIS->first(xs...))( FIT_MANGLE_CAST(const F2&)(FIT_CONST_THIS->second(xs...))(fit::forward(xs)...) ) ); }; } template struct compose_adaptor : detail::compose_kernel { typedef FIT_JOIN(compose_adaptor, Fs...) tail; typedef detail::compose_kernel base; constexpr compose_adaptor() {} template constexpr compose_adaptor(X&& f1, Xs&& ... fs) : base(fit::forward(f1), tail(fit::forward(fs)...)) {} }; template struct compose_adaptor : F { constexpr compose_adaptor() {} template constexpr compose_adaptor(X&& f1) : F(fit::forward(f1)) {} }; FIT_DECLARE_STATIC_VAR(compose, detail::make); } #endif