Skip to content

Commit da7eb50

Browse files
Merge pull request #6 from DoctorLai/fold
fold example, make format check
2 parents 3a6e779 + 9e64042 commit da7eb50

File tree

7 files changed

+167
-3
lines changed

7 files changed

+167
-3
lines changed

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ clean:
3535
@for dir in $(SUBDIRS); do \
3636
$(MAKE) -C $$dir clean; \
3737
done
38+
39+
.PHONY: check-format-all check-format
40+
41+
check-format-all:
42+
@for dir in $(SUBDIRS); do \
43+
if [ -d "$$dir" ]; then \
44+
echo "=== clang-format check in $$dir ==="; \
45+
$(MAKE) -f $(MAKEFILE_LIST) check-format DIR=$$dir; \
46+
fi; \
47+
done
48+
49+
check-format:
50+
@cd $(DIR); \
51+
echo "Checking clang-format in $(DIR)"; \
52+
if command -v bash >/dev/null 2>&1; then \
53+
bash -c 'shopt -s nullglob; files=( *.cpp *.hpp *.c *.h ); \
54+
if [ $${#files[@]} -ne 0 ]; then \
55+
clang-format --Werror --dry-run "$${files[@]}"; \
56+
else \
57+
echo "No source files found, skipping"; \
58+
fi'; \
59+
else \
60+
echo "Bash not found; skipping clang-format check"; \
61+
fi

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Examples include (and will expand to):
1919
* Views
2020
* [views-zip-enumerate](./views-zip-enumerate/)
2121
* Atomics and memory ordering
22+
* Folding
23+
* [fold-left-fold-right](./fold-left-fold-right/)
2224
* RAII and ownership patterns
2325
* Performance‑oriented C++ idioms
2426

@@ -140,6 +142,18 @@ The `clang-format` is used to ensure the code format.
140142
./clang-check.sh *.cpp *.hpp
141143
```
142144

145+
At top level, you can do:
146+
147+
```make
148+
make check-format-all
149+
```
150+
151+
At each example directory, you can do:
152+
153+
```make
154+
make check-format
155+
```
156+
143157
---
144158

145159
## Continuous Integration

fold-left-fold-right/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
CXXFLAGS += -pthread
6+
7+
TARGET := fold_left_fold_right
8+
SRCS := main.cpp
9+
OBJS := $(SRCS:.cpp=.o)
10+
11+
all: $(TARGET)
12+
13+
$(TARGET): $(OBJS)
14+
$(CXX) $(CXXFLAGS) -o $@ $^
15+
16+
%.o: %.cpp
17+
$(CXX) $(CXXFLAGS) -c $< -o $@
18+
19+
run: $(TARGET)
20+
./$(TARGET) $(ARGS)
21+
22+
clean:
23+
rm -f $(OBJS) $(TARGET)
24+
25+
# Delegates to top-level Makefile
26+
check-format:
27+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
28+
29+
.PHONY: all clean run check-format

fold-left-fold-right/main.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

thread-safe-queue/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ run: $(TARGET)
2222
clean:
2323
rm -f $(OBJS) $(TARGET)
2424

25-
.PHONY: all clean run
25+
# Delegates to top-level Makefile
26+
check-format:
27+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
28+
29+
.PHONY: all clean run check-format

unique-ptr-basics/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ run: $(TARGET)
1919
clean:
2020
rm -f $(OBJS) $(TARGET)
2121

22-
.PHONY: all clean run
22+
# Delegates to top-level Makefile
23+
check-format:
24+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
25+
26+
.PHONY: all clean run check-format

views-zip-enumerate/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ run: $(TARGET)
2222
clean:
2323
rm -f $(OBJS) $(TARGET)
2424

25-
.PHONY: all clean run
25+
# Delegates to top-level Makefile
26+
check-format:
27+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
28+
29+
.PHONY: all clean run check-format

0 commit comments

Comments
 (0)