www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Merging two arrays in a uniform order

reply aberba <karabutaworld gmail.com> writes:
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.
Jan 12 2017
parent reply ZombineDev <petar.p.kirov gmail.com> writes:
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
Jan 13 2017
parent reply aberba <karabutaworld gmail.com> writes:
On Friday, 13 January 2017 at 12:00:41 UTC, ZombineDev wrote:
 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
awesome. roundRobin? :)
Jan 13 2017
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Friday, 13 January 2017 at 19:47:38 UTC, aberba wrote:
 awesome. roundRobin? :)
https://dlang.org/phobos/std_range.html#.roundRobin [quote] roundRobin(r1, r2, r3) yields r1.front, then r2.front, then r3.front, after which it pops off one element from each and continues again from r1. For example, if two ranges are involved, it alternately yields elements off the two ranges. roundRobin stops after it has consumed all ranges (skipping over the ones that finish early). roundRobin can be used to create "interleave" functionality which inserts an element between each element in a range. [/quote]
Jan 13 2017