flatten.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*!
  2. @file
  3. Forward declares `boost::hana::flatten`.
  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_FLATTEN_HPP
  9. #define BOOST_HANA_FWD_FLATTEN_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Collapse two levels of monadic structure into a single level.
  14. //! @ingroup group-Monad
  15. //!
  16. //! Given a monadic value wrapped into two levels of monad, `flatten`
  17. //! removes one such level. An implementation of `flatten` must satisfy
  18. //! @code
  19. //! flatten(xs) == chain(xs, id)
  20. //! @endcode
  21. //!
  22. //! For `Sequence`s, this simply takes a `Sequence` of `Sequence`s, and
  23. //! returns a (non-recursively) flattened `Sequence`.
  24. //!
  25. //!
  26. //! Signature
  27. //! ---------
  28. //! For a `Monad` `M`, the signature of `flatten` is
  29. //! @f$
  30. //! \mathtt{flatten} : M(M(T)) \to M(T)
  31. //! @f$
  32. //!
  33. //! @param xs
  34. //! A value with two levels of monadic structure, which should be
  35. //! collapsed into a single level of structure.
  36. //!
  37. //!
  38. //! Example
  39. //! -------
  40. //! @include example/flatten.cpp
  41. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  42. constexpr auto flatten = [](auto&& xs) {
  43. return tag-dispatched;
  44. };
  45. #else
  46. template <typename M, typename = void>
  47. struct flatten_impl : flatten_impl<M, when<true>> { };
  48. struct flatten_t {
  49. template <typename Xs>
  50. constexpr auto operator()(Xs&& xs) const;
  51. };
  52. constexpr flatten_t flatten{};
  53. #endif
  54. BOOST_HANA_NAMESPACE_END
  55. #endif // !BOOST_HANA_FWD_FLATTEN_HPP