/*============================================================================= 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); /// /// Semantics /// --------- /// /// assert(compose(f, g)(xs...) == f(g(xs...))); /// /// Requirements /// ------------ /// /// Fs must be: /// /// * [Callable](concepts.md#callable) /// * 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 FIT_SFINAE_RESULT(const F1&, result_of...>) operator()(Ts&&... xs) const FIT_SFINAE_RETURNS ( FIT_MANGLE_CAST(const F1&)(FIT_CONST_THIS->first(xs...))( FIT_MANGLE_CAST(const F2&)(FIT_CONST_THIS->second(xs...))(FIT_FORWARD(Ts)(xs)...) ) ); }; } template struct compose_adaptor : detail::compose_kernel, FIT_JOIN(compose_adaptor, detail::callable_base...)> { typedef compose_adaptor fit_rewritable_tag; typedef FIT_JOIN(compose_adaptor, detail::callable_base...) tail; typedef detail::compose_kernel, tail> base_type; FIT_INHERIT_DEFAULT(compose_adaptor, base_type) template, X), FIT_ENABLE_IF_CONSTRUCTIBLE(tail, Xs...) > constexpr compose_adaptor(X&& f1, Xs&& ... fs) : base_type(FIT_FORWARD(X)(f1), tail(FIT_FORWARD(Xs)(fs)...)) {} template, X) > constexpr compose_adaptor(X&& f1) : base_type(FIT_FORWARD(X)(f1)) {} }; template struct compose_adaptor : detail::callable_base { typedef compose_adaptor fit_rewritable_tag; FIT_INHERIT_DEFAULT(compose_adaptor, detail::callable_base) template)> constexpr compose_adaptor(X&& f1) : detail::callable_base(FIT_FORWARD(X)(f1)) {} }; template struct compose_adaptor : detail::compose_kernel, detail::callable_base> { typedef compose_adaptor fit_rewritable_tag; typedef detail::compose_kernel, detail::callable_base> base_type; FIT_INHERIT_CONSTRUCTOR(compose_adaptor, base_type) }; FIT_DECLARE_STATIC_VAR(compose, detail::make); } // namespace fit #endif