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