scan_right.cpp 1009 B

123456789101112131415161718192021222324252627282930313233343536373839
  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/equal.hpp>
  6. #include <boost/hana/scan_right.hpp>
  7. #include <boost/hana/tuple.hpp>
  8. #include <sstream>
  9. namespace hana = boost::hana;
  10. auto to_string = [](auto x) {
  11. std::ostringstream ss;
  12. ss << x;
  13. return ss.str();
  14. };
  15. auto f = [](auto element, auto state) {
  16. return "f(" + to_string(element) + ", " + to_string(state) + ")";
  17. };
  18. int main() {
  19. // with initial state
  20. BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), 4, f) == hana::make_tuple(
  21. "f(1, f(2, f(3, 4)))",
  22. "f(2, f(3, 4))",
  23. "f(3, 4)",
  24. 4
  25. ));
  26. // without initial state
  27. BOOST_HANA_RUNTIME_CHECK(hana::scan_right(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple(
  28. "f(1, f(2, 3))",
  29. "f(2, 3)",
  30. '3'
  31. ));
  32. }