comonad.cpp 833 B

123456789101112131415161718192021222324252627282930
  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/eval.hpp>
  6. #include <boost/hana/extend.hpp>
  7. #include <boost/hana/extract.hpp>
  8. #include <boost/hana/lazy.hpp>
  9. #include <sstream>
  10. namespace hana = boost::hana;
  11. int main() {
  12. std::stringstream s("1 2 3");
  13. auto i = hana::make_lazy([&] {
  14. int i;
  15. s >> i;
  16. return i;
  17. })();
  18. auto i_plus_one = hana::extend(i, [](auto lazy_int) {
  19. return hana::eval(lazy_int) + 1;
  20. });
  21. BOOST_HANA_RUNTIME_CHECK(hana::extract(i_plus_one) == 2);
  22. BOOST_HANA_RUNTIME_CHECK(hana::extract(i_plus_one) == 3);
  23. BOOST_HANA_RUNTIME_CHECK(hana::extract(i_plus_one) == 4);
  24. }