|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <numeric> // accumulate, fold_* |
| 4 | +#include <functional> // std::plus |
| 5 | +#include <optional> |
| 6 | + |
| 7 | +template <typename Range, typename T, typename Op> |
| 8 | +T |
| 9 | +fold_left(const Range& r, T init, Op op) |
| 10 | +{ |
| 11 | + for (auto&& v : r) |
| 12 | + init = op(init, v); |
| 13 | + return init; |
| 14 | +} |
| 15 | + |
| 16 | +template <typename Range, typename T, typename Op> |
| 17 | +T |
| 18 | +fold_right(const Range& r, T init, Op op) |
| 19 | +{ |
| 20 | + for (auto it = r.rbegin(); it != r.rend(); ++it) |
| 21 | + init = op(*it, init); |
| 22 | + return init; |
| 23 | +} |
| 24 | + |
| 25 | +// fold_left_first: uses the first element of the range as the initial value |
| 26 | +template <typename Range, typename Op> |
| 27 | +std::optional<typename Range::value_type> |
| 28 | +fold_left_first(const Range& r, Op op) |
| 29 | +{ |
| 30 | + auto it = r.begin(); |
| 31 | + if (it == r.end()) |
| 32 | + return std::nullopt; |
| 33 | + auto init = *it++; |
| 34 | + for (; it != r.end(); ++it) |
| 35 | + init = op(init, *it); |
| 36 | + return init; |
| 37 | +} |
| 38 | + |
| 39 | +// fold_right_first: uses the first element of the range as the initial value |
| 40 | +template <typename Range, typename Op> |
| 41 | +std::optional<typename Range::value_type> |
| 42 | +fold_right_first(const Range& r, Op op) |
| 43 | +{ |
| 44 | + auto it = r.rbegin(); |
| 45 | + if (it == r.rend()) |
| 46 | + return std::nullopt; |
| 47 | + auto init = *it++; |
| 48 | + for (; it != r.rend(); ++it) |
| 49 | + init = op(*it, init); |
| 50 | + return init; |
| 51 | +} |
| 52 | + |
| 53 | +int |
| 54 | +main() |
| 55 | +{ |
| 56 | + std::vector<int> numbers = {1, 2, 3, 4, 5}; |
| 57 | + |
| 58 | + // Fold left (classic) |
| 59 | + int sum_left = std::accumulate(numbers.begin(), numbers.end(), 0, std::plus<int>{}); |
| 60 | + std::cout << "Fold left (sum): " << sum_left << '\n'; |
| 61 | + |
| 62 | + // Fold right (classic using reverse iterators) |
| 63 | + int sum_right = std::accumulate(numbers.rbegin(), numbers.rend(), 0, std::plus<int>{}); |
| 64 | + std::cout << "Fold right (sum): " << sum_right << '\n'; |
| 65 | + |
| 66 | + // Fold left with ranges (C++23) |
| 67 | + int sum_ranges = fold_left(numbers, 0, std::plus{}); |
| 68 | + std::cout << "Fold left with ranges (sum): " << sum_ranges << '\n'; |
| 69 | + |
| 70 | + // Fold right with ranges (C++23) |
| 71 | + int sum_ranges_right = fold_right(numbers, 0, std::plus{}); |
| 72 | + std::cout << "Fold right with ranges (sum): " << sum_ranges_right << '\n'; |
| 73 | + |
| 74 | + // Fold left using first element as initial value |
| 75 | + if (auto sum_first = fold_left_first(numbers, std::plus{})) { |
| 76 | + std::cout << "Fold left first element (sum): " << *sum_first << '\n'; |
| 77 | + } |
| 78 | + |
| 79 | + // Fold right using first element as initial value |
| 80 | + if (auto sum_right_first = fold_right_first(numbers, std::plus{})) { |
| 81 | + std::cout << "Fold right first element (sum): " << *sum_right_first << '\n'; |
| 82 | + } |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
0 commit comments