cycle.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*!
  2. @file
  3. Forward declares `boost::hana::cycle`.
  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_CYCLE_HPP
  9. #define BOOST_HANA_FWD_CYCLE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Combine a monadic structure with itself `n` times.
  14. //! @ingroup group-MonadPlus
  15. //!
  16. //! Given a monadic structure `xs` and a non-negative number `n`,
  17. //! `cycle` returns a new monadic structure which is the result of
  18. //! combining `xs` with itself `n` times using the `concat` operation.
  19. //! In other words,
  20. //! @code
  21. //! cycle(xs, n) == concat(xs, concat(xs, ... concat(xs, xs)))
  22. //! // ^^^^^ n times total
  23. //! @endcode
  24. //!
  25. //! Also note that since `concat` is required to be associative, we
  26. //! could also have written
  27. //! @code
  28. //! cycle(xs, n) == concat(concat(... concat(xs, xs), xs), xs)
  29. //! // ^^^^^ n times total
  30. //! @endcode
  31. //!
  32. //! If `n` is zero, then the identity of `concat`, `empty`, is returned.
  33. //! In the case of sequences, this boils down to returning a sequence
  34. //! containing `n` copies of itself; for other models it might differ.
  35. //!
  36. //!
  37. //! Signature
  38. //! ---------
  39. //! Given an `IntegralConstant` `C` and a `MonadPlus` `M`, the signature is
  40. //! @f$ \mathrm{cycle} : M(T) \times C \to M(T) @f$.
  41. //!
  42. //! @param xs
  43. //! A monadic structure to combine with itself a certain number of times.
  44. //!
  45. //! @param n
  46. //! A non-negative `IntegralConstant` representing the number of times to
  47. //! combine the monadic structure with itself. If `n` is zero, `cycle`
  48. //! returns `empty`.
  49. //!
  50. //!
  51. //! Example
  52. //! -------
  53. //! @include example/cycle.cpp
  54. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  55. constexpr auto cycle = [](auto&& xs, auto const& n) {
  56. return tag-dispatched;
  57. };
  58. #else
  59. template <typename M, typename = void>
  60. struct cycle_impl : cycle_impl<M, when<true>> { };
  61. struct cycle_t {
  62. template <typename Xs, typename N>
  63. constexpr auto operator()(Xs&& xs, N const& n) const;
  64. };
  65. constexpr cycle_t cycle{};
  66. #endif
  67. BOOST_HANA_NAMESPACE_END
  68. #endif // !BOOST_HANA_FWD_CYCLE_HPP