filter.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. filter.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/hof/if.hpp>
  8. #include "test.hpp"
  9. #include <boost/hof/proj.hpp>
  10. #include <boost/hof/lift.hpp>
  11. #include <boost/hof/construct.hpp>
  12. #include <boost/hof/first_of.hpp>
  13. #include <boost/hof/unpack.hpp>
  14. #include <tuple>
  15. BOOST_HOF_LIFT_CLASS(make_tuple_f, std::make_tuple);
  16. struct integer_predicate
  17. {
  18. constexpr integer_predicate()
  19. {}
  20. template<class T>
  21. constexpr auto operator()(T x) const BOOST_HOF_RETURNS
  22. (
  23. boost::hof::first_of(
  24. boost::hof::if_(std::is_integral<T>())(boost::hof::pack_basic),
  25. boost::hof::always(boost::hof::pack_basic())
  26. )(boost::hof::move(x))
  27. )
  28. };
  29. struct filter_integers
  30. {
  31. template<class Seq>
  32. constexpr auto operator()(Seq s) const BOOST_HOF_RETURNS
  33. (
  34. boost::hof::unpack(
  35. boost::hof::proj(integer_predicate(), boost::hof::unpack(make_tuple_f()))
  36. )(std::move(s))
  37. )
  38. };
  39. BOOST_HOF_TEST_CASE()
  40. {
  41. BOOST_HOF_TEST_CHECK(filter_integers()(boost::hof::pack_basic(1, 2, 2.0, 3)) == std::make_tuple(1, 2, 3));
  42. #if BOOST_HOF_HAS_CONSTEXPR_TUPLE
  43. BOOST_HOF_STATIC_TEST_CHECK(filter_integers()(boost::hof::pack_basic(1, 2, 2.0, 3)) == std::make_tuple(1, 2, 3));
  44. #endif
  45. }