ap.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. @file
  3. Forward declares `boost::hana::ap`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_AP_HPP
  9. #define BOOST_HANA_FWD_AP_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Lifted application.
  14. //! @ingroup group-Applicative
  15. //!
  16. //! Specifically, `ap` applies a structure containing functions to a
  17. //! structure containing values, and returns a new structure containing
  18. //! values. The exact way in which the functions are applied to the values
  19. //! depends on the `Applicative`.
  20. //!
  21. //! `ap` can be called with two arguments or more; the functions in the `f`
  22. //! structure are curried and then applied to the values in each `x...`
  23. //! structure using the binary form of `ap`. Note that this requires the
  24. //! number of `x...` must match the arity of the functions in the `f`
  25. //! structure. In other words, `ap(f, x1, ..., xN)` is equivalent to
  26. //! @code
  27. //! ((curry(f) ap x1) ap x2) ... ap xN
  28. //! @endcode
  29. //! where `x ap y` is just `ap(x, y)` written in infix notation to
  30. //! emphasize the left associativity.
  31. //!
  32. //!
  33. //! Signature
  34. //! ---------
  35. //! Given an Applicative `A`, the signature is
  36. //! @f$ \mathtt{ap} : A(T_1 \times \cdots \times T_n \to U)
  37. //! \times A(T_1) \times \cdots \times A(T_n)
  38. //! \to A(U) @f$.
  39. //!
  40. //! @param f
  41. //! A structure containing function(s).
  42. //!
  43. //! @param x...
  44. //! Structure(s) containing value(s) and on which `f` is applied. The
  45. //! number of structures must match the arity of the functions in the
  46. //! `f` structure.
  47. //!
  48. //!
  49. //! Example
  50. //! -------
  51. //! @include example/ap.cpp
  52. //!
  53. //! @todo
  54. //! Consider giving access to all the arguments to the tag-dispatched
  55. //! implementation for performance purposes.
  56. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  57. constexpr auto ap = [](auto&& f, auto&& ...x) -> decltype(auto) {
  58. return tag-dispatched;
  59. };
  60. #else
  61. template <typename A, typename = void>
  62. struct ap_impl : ap_impl<A, when<true>> { };
  63. struct ap_t {
  64. template <typename F, typename X>
  65. constexpr decltype(auto) operator()(F&& f, X&& x) const;
  66. template <typename F, typename ...Xs>
  67. constexpr decltype(auto) operator()(F&& f, Xs&& ...xs) const;
  68. };
  69. constexpr ap_t ap{};
  70. #endif
  71. BOOST_HANA_NAMESPACE_END
  72. #endif // !BOOST_HANA_FWD_AP_HPP