Problem with C++ ranges also exhibited by D
Atila Neves
atila.neves at gmail.com
Tue Apr 16 12:07:03 UTC 2019
This blog post shows off some problems with the ranges v3 library
that is going to be included in the C++20 standard library:
https://www.fluentcpp.com/2019/04/16/an-alternative-design-to-iterators-and-ranges-using-stdoptional/
Because the problem is basically (from the blog) "Is it really
necessary to have separate operations for advancing the iterator
and evaluating its element?". The answer is of course no, and
this has been brought up in the forums before. Like it or not, D
has the same issue:
-----------------------------
import std.algorithm;
import std.range;
import std.stdio;
void main() {
iota(1, 6)
.map!((n) { writeln("transform ", n); return n * 2; })
.filter!(n => n % 4 == 0)
.writeln;
}
-----------------------------
Produces the output:
[transform 1
transform 2
transform 2
4transform 3
transform 4
, transform 4
8transform 5
]
Showing that the mapped function is called more times than
strictly necessary, just as in C++.
More information about the Digitalmars-d
mailing list