Limitations of C++ range proposal
jmh530
john.michael.hall at gmail.com
Mon Sep 16 19:53:25 UTC 2019
I ran across this blog post on limitations of the C++ range
proposal [1] and wrote up a simple D version in virtually no time.
https://run.dlang.io/is/y6uk18
import std.range : isInputRange, ElementType;
auto interspersed(T, U)(T x, U y)
if (isInputRange!T && is(U : ElementType!T))
{
import std.range : dropBackOne;
import std.algorithm.iteration : map, joiner;
return x.map!(a => [a, y]).joiner.dropBackOne;
}
void main()
{
import std.range : iota;
import std.array : array;
auto x = iota(5);
auto y = interspersed(x, 5);
assert(y.array == [0, 5, 1, 5, 2, 5, 3, 5, 4, 5]);
}
[1]
https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
More information about the Digitalmars-d
mailing list