Static Length Propagation of Ranges
Nordlöw via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 19 06:59:29 PDT 2016
Is there a generic way to do
typeof(fn(E.init))[] map1(alias fn, E, size_t n)(E[n] container)
{
import std.algorithm.iteration : map;
import std.array : array;
return container[].map!fn.array;
}
@safe pure nothrow unittest
{
int[42] c;
auto result = map1!(_ => _^2)(c);
}
but with propagation of length of return value of `f` to a static
array instead, without having to specialize each range separately?
One less elegant way would be to replace the call to `array` with
an overload of `std.container.util.make` for static arrays, that
checks at run-time that the length of the output static array
matches the length of the input range?
More information about the Digitalmars-d-learn
mailing list