unique.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/comparing.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/tuple.hpp>
  8. #include <boost/hana/type.hpp>
  9. #include <boost/hana/unique.hpp>
  10. #include <string>
  11. namespace hana = boost::hana;
  12. using namespace std::literals;
  13. // unique without a predicate
  14. constexpr auto types = hana::tuple_t<int, float, float, char, int, int, int, double>;
  15. BOOST_HANA_CONSTANT_CHECK(
  16. hana::unique(types) == hana::tuple_t<int, float, char, int, double>
  17. );
  18. int main() {
  19. // unique with a predicate
  20. auto objects = hana::make_tuple(1, 2, "abc"s, 'd', "efg"s, "hij"s, 3.4f);
  21. BOOST_HANA_RUNTIME_CHECK(
  22. hana::unique(objects, [](auto const& t, auto const& u) {
  23. return hana::typeid_(t) == hana::typeid_(u);
  24. })
  25. == hana::make_tuple(1, "abc"s, 'd', "efg"s, 3.4f)
  26. );
  27. // unique.by is syntactic sugar
  28. BOOST_HANA_RUNTIME_CHECK(
  29. hana::unique.by(hana::comparing(hana::typeid_), objects) ==
  30. hana::make_tuple(1, "abc"s, 'd', "efg"s, 3.4f)
  31. );
  32. }