partition.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. @file
  3. Forward declares `boost::hana::partition`.
  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_PARTITION_HPP
  9. #define BOOST_HANA_FWD_PARTITION_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. //! Partition a sequence based on a `predicate`.
  15. //! @ingroup group-Sequence
  16. //!
  17. //! Specifically, returns an unspecified `Product` whose first element is
  18. //! a sequence of the elements satisfying the predicate, and whose second
  19. //! element is a sequence of the elements that do not satisfy the predicate.
  20. //!
  21. //!
  22. //! Signature
  23. //! ---------
  24. //! Given a Sequence `S(T)`, an `IntegralConstant` `Bool` holding a value
  25. //! of type `bool`, and a predicate \f$ T \to Bool \f$, `partition` has
  26. //! the following signature:
  27. //! \f[
  28. //! \mathtt{partition} : S(T) \times (T \to Bool) \to S(T) \times S(T)
  29. //! \f]
  30. //!
  31. //! @param xs
  32. //! The sequence to be partitioned.
  33. //!
  34. //! @param predicate
  35. //! A function called as `predicate(x)` for each element `x` in the
  36. //! sequence, and returning whether `x` should be added to the sequence
  37. //! in the first component or in the second component of the resulting
  38. //! pair. In the current version of the library, `predicate` must return
  39. //! an `IntegralConstant` holding a value convertible to `bool`.
  40. //!
  41. //!
  42. //! Syntactic sugar (`partition.by`)
  43. //! --------------------------------
  44. //! `partition` can be called in an alternate way, which provides a nice
  45. //! syntax in some cases where the predicate is short:
  46. //! @code
  47. //! partition.by(predicate, xs) == partition(xs, predicate)
  48. //! partition.by(predicate) == partition(-, predicate)
  49. //! @endcode
  50. //!
  51. //! where `partition(-, predicate)` denotes the partial application of
  52. //! `partition` to `predicate`.
  53. //!
  54. //!
  55. //! Example
  56. //! -------
  57. //! @include example/partition.cpp
  58. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  59. constexpr auto partition = [](auto&& xs, auto&& predicate) {
  60. return tag-dispatched;
  61. };
  62. #else
  63. template <typename S, typename = void>
  64. struct partition_impl : partition_impl<S, when<true>> { };
  65. struct partition_t : detail::nested_by<partition_t> {
  66. template <typename Xs, typename Pred>
  67. constexpr auto operator()(Xs&& xs, Pred&& pred) const;
  68. };
  69. constexpr partition_t partition{};
  70. #endif
  71. BOOST_HANA_NAMESPACE_END
  72. #endif // !BOOST_HANA_FWD_PARTITION_HPP