ap.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*!
  2. @file
  3. Forward declares `boost::hana::ap`.
  4. @copyright Louis Dionne 2013-2016
  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. //! ((f' <ap> x1) <ap> x2) ... <ap> xN
  28. //! @endcode
  29. //! where `f'` is `f` but containing curried functions instead and
  30. //! `x <ap> y` is just `ap(x, y)` written in infix notation to emphasize
  31. //! the left associativity.
  32. //!
  33. //!
  34. //! Signature
  35. //! ---------
  36. //! Given an Applicative `A`, the signature is
  37. //! @f$ \mathtt{ap} : A(T_1 \times \cdots \times T_n \to U)
  38. //! \times A(T_1) \times \cdots \times A(T_n)
  39. //! \to A(U) @f$.
  40. //!
  41. //! @param f
  42. //! A structure containing function(s).
  43. //!
  44. //! @param x...
  45. //! Structure(s) containing value(s) and on which `f` is applied. The
  46. //! number of structures must match the arity of the functions in the
  47. //! `f` structure.
  48. //!
  49. //!
  50. //! Example
  51. //! -------
  52. //! @include example/ap.cpp
  53. //!
  54. //! @todo
  55. //! Consider giving access to all the arguments to the tag-dispatched
  56. //! implementation for performance purposes.
  57. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  58. constexpr auto ap = [](auto&& f, auto&& ...x) -> decltype(auto) {
  59. return tag-dispatched;
  60. };
  61. #else
  62. template <typename A, typename = void>
  63. struct ap_impl : ap_impl<A, when<true>> { };
  64. struct ap_t {
  65. template <typename F, typename X>
  66. constexpr decltype(auto) operator()(F&& f, X&& x) const;
  67. template <typename F, typename ...Xs>
  68. constexpr decltype(auto) operator()(F&& f, Xs&& ...xs) const;
  69. };
  70. constexpr ap_t ap{};
  71. #endif
  72. BOOST_HANA_NAMESPACE_END
  73. #endif // !BOOST_HANA_FWD_AP_HPP