Skip to content

Commit

Permalink
update format
Browse files Browse the repository at this point in the history
  • Loading branch information
wusatosi committed Oct 2, 2024
1 parent d74a403 commit 4aa374c
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions examples/identity_as_default_projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
namespace exe = beman::exemplar;

// Class with a pair of values.
struct Pair
{
int n;
struct Pair {
int n;
std::string s;

// Output the pair in the form {n, s}.
// Used by the range-printer if no custom projection is provided (default: identity projection).
friend std::ostream &operator<<(std::ostream &os, const Pair &p)
{
friend std::ostream& operator<<(std::ostream& os, const Pair& p) {
return os << "Pair" << '{' << p.n << ", " << p.s << '}';
}
};
Expand All @@ -33,37 +31,28 @@ struct Pair
// All the elements of the range are printed in the form {element1, element2, ...}.
// e.g., pairs with identity: Pair{1, one}, Pair{2, two}, Pair{3, three}
// e.g., pairs with custom projection: {1:one, 2:two, 3:three}
template <std::ranges::input_range R,
typename Projection>
void print_helper(const std::string_view rem, R &&range, Projection projection)
{
template <std::ranges::input_range R, typename Projection>
void print_helper(const std::string_view rem, R&& range, Projection projection) {
std::cout << rem << '{';
std::ranges::for_each(
range,
[O = 0](const auto &o) mutable
{ std::cout << (O++ ? ", " : "") << o; },
projection);
std::ranges::for_each(range, [O = 0](const auto& o) mutable { std::cout << (O++ ? ", " : "") << o; }, projection);
std::cout << "}\n";
};

// Print wrapper with exe::identity.
template <std::ranges::input_range R,
typename Projection = exe::identity> // <- Notice the default projection.
void print_beman(const std::string_view rem, R &&range, Projection projection = {})
{
void print_beman(const std::string_view rem, R&& range, Projection projection = {}) {
print_helper(rem, range, projection);
}

// Print wrapper with std::identity.
template <std::ranges::input_range R,
typename Projection = std::identity> // <- Notice the default projection.
void print_std(const std::string_view rem, R &&range, Projection projection = {})
{
void print_std(const std::string_view rem, R&& range, Projection projection = {}) {
print_helper(rem, range, projection);
}

int main()
{
int main() {
// A vector of pairs to print.
const std::vector<Pair> pairs = {
{1, "one"},
Expand All @@ -78,12 +67,8 @@ int main()

// Print the pairs using a custom projection.
std::cout << "Custom projection:\n";
print_beman("\tpairs with beman: ", pairs,
[](const auto &p)
{ return std::to_string(p.n) + ':' + p.s; });
print_std("\tpairs with std: ", pairs,
[](const auto &p)
{ return std::to_string(p.n) + ':' + p.s; });
print_beman("\tpairs with beman: ", pairs, [](const auto& p) { return std::to_string(p.n) + ':' + p.s; });
print_std("\tpairs with std: ", pairs, [](const auto& p) { return std::to_string(p.n) + ':' + p.s; });

return 0;
}

0 comments on commit 4aa374c

Please sign in to comment.