On Saturday, 21 July 2012 at 16:42:50 UTC, Enerqi wrote:
Hello

I'm playing around with my first D program and can't figure out a way to chain a dynamic number of ranges. In this example I'm trying to chain a two dimensional array into effectively a one dimensional array, so I can later sort it as one sequence.


--------
import std.algorithm;
import std.range;

// n is determined at runtime
uint[][] arrays = new uint[][n];
foreach(ref a; arrays)
{
    a = new uint[10];
    copy(iota(10), a);
}

auto c = chain(arrays[0], arrays[1]);
foreach(a; arr[2..$])
{
    c = chain(c, a);
}
--------

Gives a compile error:
Error: cannot implicitly convert expression (chain(c,a)) of type Result to Result

any ideas?

Thanks.

The problem is that the first auto c is a chaining of an array and array, and chain(c, a) is a chaining of a chain of arrays and an array.

The way to do this is std.algorithm.joiner:

auto c = joiner(arrays, []);

Forgive me if I'm wrong (untested),

NMS

Reply via email to