How to mixin repeated text?

Andrey saasecondbox at yandex.ru
Mon Aug 27 12:27:52 UTC 2018


On Monday, 27 August 2018 at 11:56:08 UTC, Kamil Koczurek wrote:
> Mixins seem to be an overkill here. Maybe something like this 
> would suffice:
>
> data[index + 1 .. index + 5].map!(k => k[0]).array == "&&&&"

Here there is dynamic code, with memory allocs.

I found solution:
-----------------------
import std.meta;
import std.traits;
import std.stdio;
import std.array : join;
import std.algorithm.iteration : map;
import std.conv : to;
import std.range;

string qaz(alias args, alias index, ubyte count)()
{
     return iota(1, count).map!(j => args.stringof ~ '[' ~ 
index.stringof ~ '+' ~ j.to!string ~ "][0] == '&'").join("||");
}

void main()
{
     ushort index = 1;
     string[] args = ["&second", "123", "&qaz", "true", "&first", 
"77", "&value"];

     if(index + 1 >= args.length || mixin(qaz!(args, index, 4)))
     {
         writeln(qaz!(args, index, 4)());
     }
}
-----------------------


More information about the Digitalmars-d-learn mailing list