span.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. @file
  3. Forward declares `boost::hana::span`.
  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_SPAN_HPP
  9. #define BOOST_HANA_FWD_SPAN_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <boost/hana/detail/nested_by_fwd.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Returns a `Product` containing the longest prefix of a sequence
  15. //! satisfying a predicate, and the rest of the sequence.
  16. //! @ingroup group-Sequence
  17. //!
  18. //! The first component of the returned `Product` is a sequence for which
  19. //! all elements satisfy the given predicate. The second component of the
  20. //! returned `Product` is a sequence containing the remainder of the
  21. //! argument. Both or either sequences may be empty, depending on the
  22. //! input argument. More specifically,
  23. //! @code
  24. //! span(xs, predicate) == make_pair(take_while(xs, predicate),
  25. //! drop_while(xs, predicate))
  26. //! @endcode
  27. //! except that `make_pair` may be an arbitrary `Product`.
  28. //!
  29. //!
  30. //! Signature
  31. //! ---------
  32. //! Given a `Sequence` `S(T)`, a `Logical` `Bool` and a predicate
  33. //! \f$ T \to Bool \f$, `span` has the following signature:
  34. //! \f[
  35. //! \mathtt{span} : S(T) \times (T \to Bool) \to S(T) \times S(T)
  36. //! \f]
  37. //!
  38. //! @param xs
  39. //! The sequence to break into two parts.
  40. //!
  41. //! @param predicate
  42. //! A function called as `predicate(x)`, where `x` is an element of the
  43. //! sequence, and returning a `Logical. In the current implementation of
  44. //! the library, `predicate` has to return a compile-time `Logical`.
  45. //!
  46. //!
  47. //! Syntactic sugar (`span.by`)
  48. //! ---------------------------
  49. //! `span` can be called in an alternate way, which provides a nice syntax
  50. //! in some cases where the predicate is short:
  51. //! @code
  52. //! span.by(predicate, xs) == span(xs, predicate)
  53. //! span.by(predicate) == span(-, predicate)
  54. //! @endcode
  55. //!
  56. //! where `span(-, predicate)` denotes the partial application of
  57. //! `span` to `predicate`.
  58. //!
  59. //!
  60. //! Example
  61. //! -------
  62. //! @include example/span.cpp
  63. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  64. constexpr auto span = [](auto&& xs, auto&& predicate) {
  65. return tag-dispatched;
  66. };
  67. #else
  68. template <typename S, typename = void>
  69. struct span_impl : span_impl<S, when<true>> { };
  70. struct span_t : detail::nested_by<span_t> {
  71. template <typename Xs, typename Pred>
  72. constexpr auto operator()(Xs&& xs, Pred&& pred) const;
  73. };
  74. constexpr span_t span{};
  75. #endif
  76. BOOST_HANA_NAMESPACE_END
  77. #endif // !BOOST_HANA_FWD_SPAN_HPP