scan_left.cpp 1004 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_left.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 state, auto element) {
  16. return "f(" + to_string(state) + ", " + to_string(element) + ")";
  17. };
  18. int main() {
  19. // with initial state
  20. BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(2, "3", '4'), 1, f) == hana::make_tuple(
  21. 1,
  22. "f(1, 2)",
  23. "f(f(1, 2), 3)",
  24. "f(f(f(1, 2), 3), 4)"
  25. ));
  26. // without initial state
  27. BOOST_HANA_RUNTIME_CHECK(hana::scan_left(hana::make_tuple(1, "2", '3'), f) == hana::make_tuple(
  28. 1,
  29. "f(1, 2)",
  30. "f(f(1, 2), 3)"
  31. ));
  32. }