Static Length Propagation of Ranges
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 19 12:01:50 PDT 2016
On Wednesday, 19 October 2016 at 18:56:35 UTC, Meta wrote:
> Couldn't you do this? Mind you I'm not able to test the code
> right now, but I think it can work with some tweaking.
>
> typeof(fn(E.init))[n] map1(alias fn, E, size_t n)(E[n]
> container)
> {
> import std.algorithm.iteration : map;
> import std.array : array;
>
> typeof(return) sres;
> return container[].map!fn.copy(sres[]);
> }
>
> @safe pure nothrow unittest
> {
> int[42] c;
> //result should be a static array of length 42
> auto result = map1!(_ => _^2)(c);
> }
Sorry, that code had a few mistakes. The working code:
import std.algorithm;
import std.range;
typeof(fn(E.init))[n] map1(alias fn, E, size_t n)(E[n] container)
{
import std.algorithm.iteration : map;
import std.array : array;
typeof(return) sres;
container[].map!fn.copy(sres[]);
return sres;
}
void main()
{
int[42] c;
//result should be a static array of length 42
auto result = map1!(_ => _^2)(c);
assert(is(typeof(result) == typeof(c)));
}
https://goo.gl/t9m3YK
I'm actually pretty impressed that this kind of code can be
written in D.
More information about the Digitalmars-d-learn
mailing list