fold_left.cpp 944 B

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