1616namespace exe = beman::exemplar;
1717
1818// Class with a pair of values.
19- struct Pair
20- {
21- int n;
19+ struct Pair {
20+ int n;
2221 std::string s;
2322
2423 // Output the pair in the form {n, s}.
2524 // Used by the range-printer if no custom projection is provided (default: identity projection).
26- friend std::ostream &operator <<(std::ostream &os, const Pair &p)
27- {
25+ friend std::ostream& operator <<(std::ostream& os, const Pair& p) {
2826 return os << " Pair" << ' {' << p.n << " , " << p.s << ' }' ;
2927 }
3028};
@@ -33,37 +31,28 @@ struct Pair
3331// All the elements of the range are printed in the form {element1, element2, ...}.
3432// e.g., pairs with identity: Pair{1, one}, Pair{2, two}, Pair{3, three}
3533// e.g., pairs with custom projection: {1:one, 2:two, 3:three}
36- template <std::ranges::input_range R,
37- typename Projection>
38- void print_helper (const std::string_view rem, R &&range, Projection projection)
39- {
34+ template <std::ranges::input_range R, typename Projection>
35+ void print_helper (const std::string_view rem, R&& range, Projection projection) {
4036 std::cout << rem << ' {' ;
41- std::ranges::for_each (
42- range,
43- [O = 0 ](const auto &o) mutable
44- { std::cout << (O++ ? " , " : " " ) << o; },
45- projection);
37+ std::ranges::for_each (range, [O = 0 ](const auto & o) mutable { std::cout << (O++ ? " , " : " " ) << o; }, projection);
4638 std::cout << " }\n " ;
4739};
4840
4941// Print wrapper with exe::identity.
5042template <std::ranges::input_range R,
5143 typename Projection = exe::identity> // <- Notice the default projection.
52- void print_beman (const std::string_view rem, R &&range, Projection projection = {})
53- {
44+ void print_beman (const std::string_view rem, R&& range, Projection projection = {}) {
5445 print_helper (rem, range, projection);
5546}
5647
5748// Print wrapper with std::identity.
5849template <std::ranges::input_range R,
5950 typename Projection = std::identity> // <- Notice the default projection.
60- void print_std (const std::string_view rem, R &&range, Projection projection = {})
61- {
51+ void print_std (const std::string_view rem, R&& range, Projection projection = {}) {
6252 print_helper (rem, range, projection);
6353}
6454
65- int main ()
66- {
55+ int main () {
6756 // A vector of pairs to print.
6857 const std::vector<Pair> pairs = {
6958 {1 , " one" },
@@ -78,12 +67,8 @@ int main()
7867
7968 // Print the pairs using a custom projection.
8069 std::cout << " Custom projection:\n " ;
81- print_beman (" \t pairs with beman: " , pairs,
82- [](const auto &p)
83- { return std::to_string (p.n ) + ' :' + p.s ; });
84- print_std (" \t pairs with std: " , pairs,
85- [](const auto &p)
86- { return std::to_string (p.n ) + ' :' + p.s ; });
70+ print_beman (" \t pairs with beman: " , pairs, [](const auto & p) { return std::to_string (p.n ) + ' :' + p.s ; });
71+ print_std (" \t pairs with std: " , pairs, [](const auto & p) { return std::to_string (p.n ) + ' :' + p.s ; });
8772
8873 return 0 ;
8974}
0 commit comments