prepend.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*!
  2. @file
  3. Forward declares `boost::hana::prepend`.
  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_PREPEND_HPP
  9. #define BOOST_HANA_FWD_PREPEND_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Prepend an element to a monadic structure.
  14. //! @ingroup group-MonadPlus
  15. //!
  16. //! Given a monadic structure `xs` and an element `x`, `prepend` returns
  17. //! a new monadic structure which is the result of lifting `x` into the
  18. //! monadic structure and then combining that (to the left) with `xs`.
  19. //! In other words,
  20. //! @code
  21. //! prepend(xs, x) == concat(lift<Xs>(x), xs)
  22. //! @endcode
  23. //!
  24. //! For sequences, this has the intuitive behavior of simply prepending
  25. //! an element to the beginning of the sequence, hence the name.
  26. //!
  27. //! > #### Rationale for not calling this `push_front`
  28. //! > While `push_front` is the de-facto name used in the standard library,
  29. //! > it also strongly suggests mutation of the underlying sequence, which
  30. //! > is not the case here. The author also finds that `push_front`
  31. //! > suggests too strongly the sole interpretation of putting an
  32. //! > element to the front of a sequence, whereas `prepend` is slightly
  33. //! > more nuanced and bears its name better for e.g. `hana::optional`.
  34. //!
  35. //!
  36. //! Signature
  37. //! ---------
  38. //! Given a MonadPlus `M`, the signature is
  39. //! @f$ \mathtt{prepend} : M(T) \times T \to M(T) @f$.
  40. //!
  41. //! @param xs
  42. //! A monadic structure that will be combined to the right of the element.
  43. //!
  44. //! @param x
  45. //! An element to combine to the left of the monadic structure.
  46. //!
  47. //!
  48. //! Example
  49. //! -------
  50. //! @include example/prepend.cpp
  51. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  52. constexpr auto prepend = [](auto&& xs, auto&& x) {
  53. return tag-dispatched;
  54. };
  55. #else
  56. template <typename M, typename = void>
  57. struct prepend_impl : prepend_impl<M, when<true>> { };
  58. struct prepend_t {
  59. template <typename Xs, typename X>
  60. constexpr auto operator()(Xs&& xs, X&& x) const;
  61. };
  62. constexpr prepend_t prepend{};
  63. #endif
  64. BOOST_HANA_NAMESPACE_END
  65. #endif // !BOOST_HANA_FWD_PREPEND_HPP