/*============================================================================= 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 namespace fit { namespace detail { // TODO: Try to use inheritance but make each base class unique template struct compose_kernel { F1 f1; F2 f2; constexpr compose_kernel() {} template constexpr compose_kernel(A&& f1, B&& f2) : f1(fit::forward(f1)), f2(fit::forward(f2)) {} FIT_RETURNS_CLASS(compose_kernel); template constexpr auto operator()(Ts&&... xs) const FIT_RETURNS ( FIT_CONST_THIS->f1(FIT_CONST_THIS->f2(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)) {} }; template constexpr FIT_JOIN(compose_adaptor, Fs...) compose(Fs... fs) { return FIT_JOIN(compose_adaptor, Fs...)(fit::move(fs)...); } } #endif