unfold_right.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. @file
  3. Forward declares `boost::hana::unfold_right`.
  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_UNFOLD_RIGHT_HPP
  9. #define BOOST_HANA_FWD_UNFOLD_RIGHT_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Dual operation to `fold_right` for sequences.
  14. //! @ingroup group-Sequence
  15. //!
  16. //! While `fold_right` reduces a structure to a summary value from the
  17. //! right, `unfold_right` builds a sequence from a seed value and a
  18. //! function, starting from the right.
  19. //!
  20. //!
  21. //! Signature
  22. //! ---------
  23. //! Given a `Sequence` `S`, an initial value `state` of tag `I`, an
  24. //! arbitrary Product `P` and a function \f$ f : I \to P(T, I) \f$,
  25. //! `unfold_right<S>` has the following signature:
  26. //! \f[
  27. //! \mathtt{unfold\_right}_S : I \times (I \to P(T, I)) \to S(T)
  28. //! \f]
  29. //!
  30. //! @tparam S
  31. //! The tag of the sequence to build up.
  32. //!
  33. //! @param state
  34. //! An initial value to build the sequence from.
  35. //!
  36. //! @param f
  37. //! A function called as `f(state)`, where `state` is an initial value,
  38. //! and returning
  39. //! 1. `nothing` if it is done producing the sequence.
  40. //! 2. otherwise, `just(make<P>(x, state))`, where `state` is the new
  41. //! initial value used in the next call to `f`, `x` is an element to
  42. //! be prepended to the resulting sequence, and `P` is an arbitrary
  43. //! `Product`.
  44. //!
  45. //!
  46. //! Fun fact
  47. //! ---------
  48. //! In some cases, `unfold_right` can undo a `fold_right` operation:
  49. //! @code
  50. //! unfold_right<S>(fold_right(xs, state, f), g) == xs
  51. //! @endcode
  52. //!
  53. //! if the following holds
  54. //! @code
  55. //! g(f(x, y)) == just(make_pair(x, y))
  56. //! g(state) == nothing
  57. //! @endcode
  58. //!
  59. //!
  60. //! Example
  61. //! -------
  62. //! @include example/unfold_right.cpp
  63. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  64. template <typename S>
  65. constexpr auto unfold_right = [](auto&& state, auto&& f) {
  66. return tag-dispatched;
  67. };
  68. #else
  69. template <typename S, typename = void>
  70. struct unfold_right_impl : unfold_right_impl<S, when<true>> { };
  71. template <typename S>
  72. struct unfold_right_t;
  73. template <typename S>
  74. constexpr unfold_right_t<S> unfold_right{};
  75. #endif
  76. BOOST_HANA_NAMESPACE_END
  77. #endif // !BOOST_HANA_FWD_UNFOLD_RIGHT_HPP