cplusplus / cplusplus/draft

Change range adaptor examples to print with formatting ranges

Open
#5,908 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

Currently, every range adaptor starts with a very short example that prints the contents of an adapted range. For instance, transform has:

vector<int> is{ 0, 1, 2, 3, 4 };
auto squares = views::transform(is, [](int i) { return i * i; });
for (int i : squares)
  cout << i << ' '; // prints 0 1 4 9 16

adjacent has:

vector v = {1, 2, 3, 4};

for (auto i : v | views::adjacent<2>) {
  cout << "(" << std::get<0>(i) << ", " << std::get<1>(i) << ") ";  // prints (1, 2) (2, 3) (3, 4)
}

chunk has:

vector v = {1, 2, 3, 4, 5};

for (auto r : v | views::chunk(2)) {
  cout << '[';
  auto sep = "";
  for (auto i : r) {
    cout << sep << i;
    sep = ", ";
  }
  cout << "] ";
}
// The above prints [1, 2] [3, 4] [5]

We can now change all of these loops (some of which are nested) to just a call to print, which will make the examples smaller and also provide more information. For instance, the adjacent example doesn't actually make clear that it gives you a 2-tuple back. The above can be turned into:

vector<int> is{ 0, 1, 2, 3, 4 };
auto squares = views::transform(is, [](int i) { return i * i; });
print("{}", squares); // prints [0, 1, 4, 9, 16]

and

vector v = {1, 2, 3, 4};
print("{}", v | views::adjacent<2>); // prints [(1, 2), (2, 3), (3, 4)]

and

vector v = {1, 2, 3, 4, 5};
print("{}", v | views::chunk(2)); // prints [[1, 2], [3, 4], [5]]

Is there any reason to not do this?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the range adaptor examples for transform, adjacent, chunk, and the other adaptors in the draft. Review how the existing examples print adapted ranges and how formatting ranges are represented, then update the applicable loops to use print. Done means the examples are shorter, consistently formatted, and clearly show the resulting range structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, tex
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.