replicate.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*!
  2. @file
  3. Forward declares `boost::hana::replicate`.
  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_REPLICATE_HPP
  9. #define BOOST_HANA_FWD_REPLICATE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Create a monadic structure by combining a lifted value with itself
  14. //! `n` times.
  15. //! @ingroup group-MonadPlus
  16. //!
  17. //! Given a value `x`, a non-negative `IntegralConstant` `n` and the tag
  18. //! of a monadic structure `M`, `replicate` creates a new monadic structure
  19. //! which is the result of combining `x` with itself `n` times inside the
  20. //! monadic structure. In other words, `replicate` simply `lift`s `x` into
  21. //! the monadic structure, and then combines that with itself `n` times:
  22. //! @code
  23. //! replicate<M>(x, n) == cycle(lift<M>(x), n)
  24. //! @endcode
  25. //!
  26. //! If `n` is zero, then the identity of the `concat` operation is returned.
  27. //! In the case of sequences, this corresponds to creating a new sequence
  28. //! holding `n` copies of `x`.
  29. //!
  30. //!
  31. //! Signature
  32. //! ---------
  33. //! Given an `IntegralConstant` `C` and MonadPlus `M`, the signature is
  34. //! @f$ \mathtt{replicate}_M : T \times C \to M(T) @f$.
  35. //!
  36. //! @tparam M
  37. //! The tag of the returned monadic structure. It must be a
  38. //! model of the MonadPlus concept.
  39. //!
  40. //! @param x
  41. //! The value to lift into a monadic structure and then combine with
  42. //! itself.
  43. //!
  44. //! @param n
  45. //! A non-negative `IntegralConstant` representing the number of times to
  46. //! combine `lift<M>(x)` with itself. If `n == 0`, `replicate` returns
  47. //! `empty<M>()`.
  48. //!
  49. //!
  50. //! Example
  51. //! -------
  52. //! @include example/replicate.cpp
  53. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  54. template <typename M>
  55. constexpr auto replicate = [](auto&& x, auto const& n) {
  56. return tag-dispatched;
  57. };
  58. #else
  59. template <typename M, typename = void>
  60. struct replicate_impl : replicate_impl<M, when<true>> { };
  61. template <typename M>
  62. struct replicate_t {
  63. template <typename X, typename N>
  64. constexpr auto operator()(X&& x, N const& n) const;
  65. };
  66. template <typename M>
  67. constexpr replicate_t<M> replicate{};
  68. #endif
  69. BOOST_HANA_NAMESPACE_END
  70. #endif // !BOOST_HANA_FWD_REPLICATE_HPP