range-v3 icon indicating copy to clipboard operation
range-v3 copied to clipboard

transform is call twice for each element in range in case of combination with filter

Open sepcon opened this issue 4 years ago • 2 comments

I have a sample: ` using namespace ranges; namespace rv = ranges::views;

vector<int> vec = { 1, 2, 3 };


auto output = vec
	| rv::transform([](int i) {
	cout << "transform: " << i << endl;
	return i + 1;
		}) | rv::filter([](int i) {
			cout << "filter0: " << i << endl;
			return i > 0;
			});

		for (auto r : output) {
			cout << r << endl;
		}`

The output is: transform: 1 filter0: 2 transform: 1 2 transform: 2 filter0: 3 transform: 2 3 transform: 3 filter0: 4 transform: 3 4

  1. From output we can see: transform: is printed twice for each value --> this should be considered as BUG, right?
  2. If apply filter before transform then transform is applied only once: filter0: 1 transform: 1 2 filter0: 2 transform: 2 3 filter0: 3 transform: 3 4

sepcon avatar Mar 15 '21 02:03 sepcon

It isn't a bug. Search for twice and you get similar issues: https://github.com/ericniebler/range-v3/issues?q=twice.

JohelEGP avatar Mar 15 '21 02:03 JohelEGP

There is ranges::views::cache1 to prevent calling the transform function twice.

sv1990 avatar Mar 15 '21 18:03 sv1990