monadic_compose.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. @file
  3. Forward declares `boost::hana::monadic_compose`.
  4. Copyright Louis Dionne 2013-2022
  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_MONADIC_COMPOSE_HPP
  9. #define BOOST_HANA_FWD_MONADIC_COMPOSE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. namespace boost { namespace hana {
  13. //! Composition of monadic functions.
  14. //! @ingroup group-Monad
  15. //!
  16. //! Given two monadic functions `f` and `g`, `monadic_compose` returns
  17. //! a new function equivalent to the composition of `f` with `g`, except
  18. //! the result of `g` is `chain`ed into `f` instead of simply passed to
  19. //! it, as with normal composition. `monadic_compose` satisfies
  20. //! @code
  21. //! monadic_compose(f, g)(x) == chain(g(x), f)
  22. //! @endcode
  23. //!
  24. //!
  25. //! @note
  26. //! Unlike `compose`, `monadic_compose` does not generalize nicely to
  27. //! arities higher than one. Hence, only unary functions may be used
  28. //! with `monadic_compose`.
  29. //!
  30. //!
  31. //! Signature
  32. //! ---------
  33. //! Given a `Monad` `M` and two functions @f$ f : B \to M(C) @f$ and
  34. //! @f$ g : A \to M(B) @f$, the signature is
  35. //! @f$
  36. //! \mathtt{monadic\_compose}
  37. //! : (B \to M(C)) \times (A \to M(B)) \to (A \to M(C))
  38. //! @f$.
  39. //!
  40. //! @param f
  41. //! A monadic function with signature @f$ B \to M(C) @f$.
  42. //!
  43. //! @param g
  44. //! A monadic function with signature @f$ A \to M(B) @f$.
  45. //!
  46. //!
  47. //! @note
  48. //! This method is not tag-dispatched, so it can't be customized directly.
  49. //!
  50. //!
  51. //! Example
  52. //! -------
  53. //! @include example/monadic_compose.cpp
  54. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  55. constexpr auto monadic_compose = [](auto&& f, auto&& g) {
  56. return [perfect-capture](auto&& x) -> decltype(auto) {
  57. return hana::chain(forwarded(g)(forwarded(x)), forwarded(f));
  58. };
  59. };
  60. #else
  61. struct monadic_compose_t {
  62. template <typename F, typename G>
  63. constexpr auto operator()(F&& f, G&& g) const;
  64. };
  65. BOOST_HANA_INLINE_VARIABLE constexpr monadic_compose_t monadic_compose{};
  66. #endif
  67. }} // end namespace boost::hana
  68. #endif // !BOOST_HANA_FWD_MONADIC_COMPOSE_HPP