suffix.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. @file
  3. Forward declares `boost::hana::suffix`.
  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_SUFFIX_HPP
  9. #define BOOST_HANA_FWD_SUFFIX_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. namespace boost { namespace hana {
  13. //! Inserts a value after each element of a monadic structure.
  14. //! @ingroup group-MonadPlus
  15. //!
  16. //! Given a monadic structure `xs` and a value `z` (called the suffix),
  17. //! `suffix` returns a new monadic structure such that
  18. //! @code
  19. //! suffix(xs, z) == flatten(transform(xs, [](auto x) {
  20. //! return concat(lift<M>(x), lift<M>(z));
  21. //! }))
  22. //! @endcode
  23. //!
  24. //! For sequences, this simply corresponds to inserting the suffix after
  25. //! each element of the sequence. For example, given a sequence
  26. //! `[x1, ..., xn]`, `suffix` will return
  27. //! @code
  28. //! [x1, z, x2, z, ..., xn, z]
  29. //! @endcode
  30. //! As explained above, this can be generalized to other MonadPlus models,
  31. //! with various levels of interest.
  32. //!
  33. //!
  34. //! Signature
  35. //! ---------
  36. //! Given a MonadPlus `M`, the signature is
  37. //! @f$ \mathtt{suffix} : M(T) \times T \to M(T) @f$.
  38. //!
  39. //! @param xs
  40. //! A monadic structure.
  41. //!
  42. //! @param sfx
  43. //! A value (the suffix) to insert after each element of a monadic
  44. //! structure.
  45. //!
  46. //!
  47. //! Example
  48. //! -------
  49. //! @include example/suffix.cpp
  50. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  51. constexpr auto suffix = [](auto&& xs, auto&& sfx) {
  52. return tag-dispatched;
  53. };
  54. #else
  55. template <typename M, typename = void>
  56. struct suffix_impl : suffix_impl<M, when<true>> { };
  57. struct suffix_t {
  58. template <typename Xs, typename Sfx>
  59. constexpr auto operator()(Xs&& xs, Sfx&& sfx) const;
  60. };
  61. BOOST_HANA_INLINE_VARIABLE constexpr suffix_t suffix{};
  62. #endif
  63. }} // end namespace boost::hana
  64. #endif // !BOOST_HANA_FWD_SUFFIX_HPP