diff --git a/include/boost/hana/iterable.hpp b/include/boost/hana/iterable.hpp index b1de96de0..1b2e5c3ca 100644 --- a/include/boost/hana/iterable.hpp +++ b/include/boost/hana/iterable.hpp @@ -57,6 +57,12 @@ namespace boost { namespace hana { { return Iterable::length_impl(iterable); } } length{}; + constexpr struct _drop { + template + constexpr auto operator()(N n, Iterable_ iterable) const + { return Iterable::drop_impl(n, iterable); } + } drop{}; + template <> struct defaults { @@ -93,6 +99,21 @@ namespace boost { namespace hana { template static constexpr auto length_impl(Iterable_ iterable) { return length_helper(iterable, is_empty(iterable)); } + + + template + static constexpr auto drop_helper(N, Iterable_ iterable, Bool) + { return iterable; } + + template + static constexpr auto drop_helper(N n, Iterable_ iterable, Bool) + { return drop(n - int_<1>, tail(iterable)); } + + template + static constexpr auto drop_impl(N n, Iterable_ iterable) { + return drop_helper(n, iterable, + n == int_<0> || is_empty(iterable)); + } }; }} // end namespace boost::hana diff --git a/test/iterable.cpp b/test/iterable.cpp index 33aad5e39..66d7c82ca 100644 --- a/test/iterable.cpp +++ b/test/iterable.cpp @@ -39,8 +39,23 @@ void test_length() { BOOST_HANA_STATIC_ASSERT(length(iterable(int_<0>, int_<1>, int_<2>)) == int_<3>); } +void test_drop() { + BOOST_HANA_STATIC_ASSERT(drop(int_<0>, iterable()) == iterable()); + BOOST_HANA_STATIC_ASSERT(drop(int_<1>, iterable()) == iterable()); + BOOST_HANA_STATIC_ASSERT(drop(int_<2>, iterable()) == iterable()); + + BOOST_HANA_STATIC_ASSERT(drop(int_<0>, iterable(int_<0>)) == iterable(int_<0>)); + BOOST_HANA_STATIC_ASSERT(drop(int_<1>, iterable(int_<0>)) == iterable()); + BOOST_HANA_STATIC_ASSERT(drop(int_<2>, iterable(int_<0>)) == iterable()); + + BOOST_HANA_STATIC_ASSERT(drop(int_<0>, iterable(int_<0>, int_<1>)) == iterable(int_<0>, int_<1>)); + BOOST_HANA_STATIC_ASSERT(drop(int_<1>, iterable(int_<0>, int_<1>)) == iterable(int_<1>)); + BOOST_HANA_STATIC_ASSERT(drop(int_<2>, iterable(int_<0>, int_<1>)) == iterable()); +} + int main() { test_at(); test_last(); test_length(); + test_drop(); }