Static Length Propagation of Ranges
Nordlöw via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 20 05:38:40 PDT 2016
On Wednesday, 19 October 2016 at 19:39:46 UTC, Nordlöw wrote:
> On Wednesday, 19 October 2016 at 19:01:50 UTC, Meta wrote:
>> https://goo.gl/t9m3YK
>>
>> I'm actually pretty impressed that this kind of code can be
>> written in D.
>
> Thanks! Add at
>
> https://github.com/nordlow/phobos-next/blob/master/src/algorithm_ex.d#L2234
Made it even modular by factoring out arrayN at
https://github.com/nordlow/phobos-next/blob/master/src/algorithm_ex.d#L2200
and used at
https://github.com/nordlow/phobos-next/blob/master/src/algorithm_ex.d#L2215
Code:
ElementType!R[n] arrayN(size_t n, R)(R r)
{
assert(r.length == n);
typeof(return) dst;
import std.algorithm.mutation : copy;
r.copy(dst[]);
return dst;
}
typeof(fun(E.init))[n] map(alias fun, E, size_t n)(const E[n] src)
{
import std.algorithm.iteration : map;
return src[].map!fun.arrayN!n;
}
@safe pure nothrow unittest
{
import std.meta : AliasSeq;
foreach (E; AliasSeq!(int, double))
{
enum n = 42;
E[n] c;
const result = map!(_ => _^^2)(c);
static assert(c.length == result.length);
static assert(is(typeof(result) == const(E)[n]));
}
}
More information about the Digitalmars-d-learn
mailing list