/* @copyright Louis Dionne 2014 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #include #include #include #include #include #include #include #include #include using namespace boost::hana; struct LazyList; template struct lazy_cons_type { X x; Xs xs; using hana_datatype = LazyList; }; auto lazy_cons = [](auto x, auto xs) { return lazy_cons_type{x, xs}; }; struct lazy_nil_type { using hana_datatype = LazyList; }; constexpr lazy_nil_type lazy_nil{}; auto repeat = fix([](auto repeat, auto x) { return lazy_cons(x, lazy(repeat)(x)); }); namespace boost { namespace hana { template <> struct Iterable::instance : Iterable::mcd { template static constexpr auto head_impl(Xs lcons) { return lcons.x; } template static constexpr auto tail_impl(Xs lcons) { return eval(lcons.xs); } template static constexpr auto is_empty_impl(Xs lcons) { return false_; } static constexpr auto is_empty_impl(lazy_nil_type) { return true_; } }; template <> struct List::instance : List::mcd { static constexpr auto nil_impl() { return lazy_nil; } template static constexpr auto cons_impl(X x, Xs xs) { return lazy_cons(x, lazy(xs)); } }; }} int main() { BOOST_HANA_STATIC_ASSERT(!is_empty(repeat(1))); BOOST_HANA_STATIC_ASSERT(head(repeat(1)) == 1); BOOST_HANA_STATIC_ASSERT(at(int_<10>, repeat(1)) == 1); BOOST_HANA_STATIC_ASSERT(take(int_<2>, repeat('x')) == list('x', 'x')); }