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