prefix.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. @file
  3. Forward declares `boost::hana::prefix`.
  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_PREFIX_HPP
  9. #define BOOST_HANA_FWD_PREFIX_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Inserts a value before each element of a monadic structure.
  14. //! @ingroup group-MonadPlus
  15. //!
  16. //! Given a monadic structure `xs` and a value `z` called the prefix,
  17. //! `prefix` returns a new monadic structure. `prefix` satisfies
  18. //! @code
  19. //! prefix(xs, z) == flatten(transform(xs, [](auto x) {
  20. //! return concat(lift<M>(z), lift<M>(x));
  21. //! }))
  22. //! @endcode
  23. //!
  24. //! For sequences, this simply corresponds to inserting the prefix before
  25. //! each element of the sequence. For example, given a sequence
  26. //! `[x1, ..., xn]`, `prefix` will return
  27. //! @code
  28. //! [z, x1, z, x2, ..., z, xn]
  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$ \mathrm{prefix} : M(T) \times T \to M(T) @f$.
  38. //!
  39. //! @param xs
  40. //! A monadic structure.
  41. //!
  42. //! @param pref
  43. //! A value (the prefix) to insert before each element of a monadic
  44. //! structure.
  45. //!
  46. //!
  47. //! Example
  48. //! -------
  49. //! @include example/prefix.cpp
  50. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  51. constexpr auto prefix = [](auto&& xs, auto&& pref) {
  52. return tag-dispatched;
  53. };
  54. #else
  55. template <typename M, typename = void>
  56. struct prefix_impl : prefix_impl<M, when<true>> { };
  57. struct prefix_t {
  58. template <typename Xs, typename Pref>
  59. constexpr auto operator()(Xs&& xs, Pref&& pref) const;
  60. };
  61. constexpr prefix_t prefix{};
  62. #endif
  63. BOOST_HANA_NAMESPACE_END
  64. #endif // !BOOST_HANA_FWD_PREFIX_HPP