monadic_fold_left.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*!
  2. @file
  3. Defines `boost::hana::monadic_fold_left`.
  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_MONADIC_FOLD_LEFT_HPP
  9. #define BOOST_HANA_MONADIC_FOLD_LEFT_HPP
  10. #include <boost/hana/fwd/monadic_fold_left.hpp>
  11. #include <boost/hana/chain.hpp>
  12. #include <boost/hana/concept/foldable.hpp>
  13. #include <boost/hana/concept/monad.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/decay.hpp>
  17. #include <boost/hana/fold_right.hpp>
  18. #include <boost/hana/functional/curry.hpp>
  19. #include <boost/hana/functional/partial.hpp>
  20. #include <boost/hana/lift.hpp>
  21. #include <type_traits>
  22. BOOST_HANA_NAMESPACE_BEGIN
  23. //! @cond
  24. template <typename M>
  25. template <typename Xs, typename State, typename F>
  26. constexpr decltype(auto) monadic_fold_left_t<M>::operator()(Xs&& xs, State&& state, F&& f) const {
  27. using S = typename hana::tag_of<Xs>::type;
  28. using MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_impl<S>,
  29. hana::Foldable<S>::value
  30. );
  31. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  32. static_assert(hana::Monad<M>::value,
  33. "hana::monadic_fold_left<M> requires 'M' to be a Monad");
  34. static_assert(hana::Foldable<S>::value,
  35. "hana::monadic_fold_left<M>(xs, state, f) requires 'xs' to be Foldable");
  36. #endif
  37. return MonadicFoldLeft::template apply<M>(static_cast<Xs&&>(xs),
  38. static_cast<State&&>(state),
  39. static_cast<F&&>(f));
  40. }
  41. //! @endcond
  42. //! @cond
  43. template <typename M>
  44. template <typename Xs, typename F>
  45. constexpr decltype(auto) monadic_fold_left_t<M>::operator()(Xs&& xs, F&& f) const {
  46. using S = typename hana::tag_of<Xs>::type;
  47. using MonadicFoldLeft = BOOST_HANA_DISPATCH_IF(monadic_fold_left_impl<S>,
  48. hana::Foldable<S>::value
  49. );
  50. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  51. static_assert(hana::Monad<M>::value,
  52. "hana::monadic_fold_left<M> requires 'M' to be a Monad");
  53. static_assert(hana::Foldable<S>::value,
  54. "hana::monadic_fold_left<M>(xs, f) requires 'xs' to be Foldable");
  55. #endif
  56. return MonadicFoldLeft::template apply<M>(static_cast<Xs&&>(xs),
  57. static_cast<F&&>(f));
  58. }
  59. //! @endcond
  60. namespace detail {
  61. struct foldlM_helper {
  62. template <typename F, typename X, typename K, typename Z>
  63. constexpr decltype(auto) operator()(F&& f, X&& x, K&& k, Z&& z) const {
  64. return hana::chain(
  65. static_cast<F&&>(f)(
  66. static_cast<Z&&>(z),
  67. static_cast<X&&>(x)
  68. ),
  69. static_cast<K&&>(k)
  70. );
  71. }
  72. };
  73. template <typename End, typename M, typename F>
  74. struct monadic_foldl1_helper {
  75. F f;
  76. template <typename X, typename Y>
  77. constexpr decltype(auto) operator()(X&& x, Y&& y) const
  78. { return f(static_cast<X&&>(x), static_cast<Y&&>(y)); }
  79. template <typename Y>
  80. constexpr decltype(auto) operator()(End, Y&& y) const
  81. { return hana::lift<M>(static_cast<Y&&>(y)); }
  82. };
  83. }
  84. template <typename T, bool condition>
  85. struct monadic_fold_left_impl<T, when<condition>> : default_ {
  86. // with state
  87. template <typename M, typename Xs, typename S, typename F>
  88. static constexpr decltype(auto) apply(Xs&& xs, S&& s, F&& f) {
  89. return hana::fold_right(
  90. static_cast<Xs&&>(xs),
  91. hana::lift<M>,
  92. hana::curry<3>(hana::partial(
  93. detail::foldlM_helper{}, static_cast<F&&>(f)
  94. ))
  95. )(static_cast<S&&>(s));
  96. }
  97. // without state
  98. template <typename M, typename Xs, typename F>
  99. static constexpr decltype(auto) apply(Xs&& xs, F&& f) {
  100. struct end { };
  101. using G = detail::monadic_foldl1_helper<end, M, typename detail::decay<F>::type>;
  102. decltype(auto) result = hana::monadic_fold_left<M>(
  103. static_cast<Xs&&>(xs),
  104. end{},
  105. G{static_cast<F&&>(f)}
  106. );
  107. static_assert(!std::is_same<
  108. std::remove_reference_t<decltype(result)>,
  109. decltype(hana::lift<M>(end{}))
  110. >{},
  111. "hana::monadic_fold_left<M>(xs, f) requires 'xs' to be non-empty");
  112. return result;
  113. }
  114. };
  115. BOOST_HANA_NAMESPACE_END
  116. #endif // !BOOST_HANA_MONADIC_FOLD_LEFT_HPP