3 Defines `boost::hana::ap`.
5 @copyright Louis Dionne 2013-2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_AP_HPP
11 #define BOOST_HANA_AP_HPP
13 #include <boost/hana/fwd/ap.hpp>
15 #include <boost/hana/chain.hpp>
16 #include <boost/hana/concept/applicative.hpp>
17 #include <boost/hana/concept/sequence.hpp>
18 #include <boost/hana/config.hpp>
19 #include <boost/hana/core/dispatch.hpp>
20 #include <boost/hana/detail/variadic/foldl1.hpp>
21 #include <boost/hana/functional/curry.hpp>
22 #include <boost/hana/functional/partial.hpp>
23 #include <boost/hana/transform.hpp>
26 BOOST_HANA_NAMESPACE_BEGIN
27 template <typename A, bool condition>
28 struct ap_impl<A, when<condition>> : default_ {
29 template <typename ...Args>
30 static constexpr auto apply(Args&& ...args) = delete;
34 template <typename F, typename X>
35 constexpr decltype(auto) ap_t::operator()(F&& f, X&& x) const {
36 using Function = typename hana::tag_of<F>::type;
37 using Value = typename hana::tag_of<X>::type;
38 using Ap = BOOST_HANA_DISPATCH_IF(ap_impl<Function>,
39 hana::Applicative<Function>::value && hana::Applicative<Value>::value
42 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
43 static_assert(hana::Applicative<Function>::value,
44 "hana::ap(f, x) requires 'f' to be an Applicative");
46 static_assert(hana::Applicative<Value>::value,
47 "hana::ap(f, x) requires 'x' to be an Applicative");
50 return Ap::apply(static_cast<F&&>(f), static_cast<X&&>(x));
53 template <typename F, typename ...Xs>
54 constexpr decltype(auto) ap_t::operator()(F&& f, Xs&& ...xs) const {
55 static_assert(sizeof...(xs) >= 1,
56 "hana::ap must be called with at least two arguments");
58 return detail::variadic::foldl1(
60 hana::transform(static_cast<F&&>(f), hana::curry<sizeof...(xs)>),
61 static_cast<Xs&&>(xs)...
67 struct ap_impl<S, when<Sequence<S>::value>> {
68 template <typename F, typename X>
69 static constexpr decltype(auto) apply(F&& f, X&& x) {
72 hana::partial(hana::transform, static_cast<X&&>(x))
76 BOOST_HANA_NAMESPACE_END
78 #endif // !BOOST_HANA_AP_HPP