/* @copyright Louis Dionne 2015 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #include #include #include #include using namespace boost::hana; //! [applicative] template constexpr auto function = nothing; template <> BOOST_HANA_CONSTEXPR_LAMBDA auto function<'+'> = just([](auto x, auto y) { return x + y; }); template <> BOOST_HANA_CONSTEXPR_LAMBDA auto function<'-'> = just([](auto x, auto y) { return x - y; }); // and so on... template BOOST_HANA_CONSTEXPR_LAMBDA auto digit = if_(bool_<(n >= '0' && n <= '9')>, just(static_cast(n - 48)), nothing ); template BOOST_HANA_CONSTEXPR_LAMBDA auto evaluate = ap(function, digit, digit); int main() { BOOST_HANA_CONSTEXPR_CHECK(evaluate<'1', '+', '2'> == just(1 + 2)); BOOST_HANA_CONSTANT_CHECK(evaluate<'?', '+', '2'> == nothing); BOOST_HANA_CONSTANT_CHECK(evaluate<'1', '?', '2'> == nothing); BOOST_HANA_CONSTANT_CHECK(evaluate<'1', '+', '?'> == nothing); BOOST_HANA_CONSTANT_CHECK(evaluate<'?', '?', '?'> == nothing); BOOST_HANA_CONSTEXPR_CHECK(lift(123) == just(123)); } //! [applicative]