Merging two arrays in a uniform order

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 13 04:00:41 PST 2017


On Friday, 13 January 2017 at 06:32:02 UTC, aberba wrote:
> Unlike array1 + array2, how can i merge arrays such that:
>
> [a1, a1, a2, a1, a1, a2, a1] //uniform order
>
> where a1 = child of array1,
> a2 = child of array2
>
> using a built-in function/algorithm (is/are there anything(s) 
> in Phobos for this?). No manual approach.

void main()
{
     import std.stdio : writeln;
     import std.range : refRange, roundRobin;

     auto array1 = [1, 2, 3, 4, 5];
     auto array2 = [11, 12, 13, 14, 15];

     auto array1Ref = refRange(&array1);

     roundRobin(array1Ref, array1Ref, array2)
         .writeln();
}

elements: [ 1,  2,  11,  3,   4,  12,   5,  13,  14,  15  ]
indexes:   a11 a12 a21  a13  a14  a22  a15  a23  a24  a25


More information about the Digitalmars-d-learn mailing list