Thanks! That does join up the arrays as I'd like.
An issue remaining is that, unlike with the chain function, I can't sort the output of the joiner function.

Error: template instance std.algorithm.sort!("a <
b",cast(SwapStrategy)0,Result) error instantiating

Seems the return type of joiner doesn't implement random access in the same way the return type of chain does. Chain's documentation says "If all input ranges offer random access and $(D
length), $(D Chain) offers them as well."

I wonder if joiner is sortable somehow?


On Saturday, 21 July 2012 at 17:00:02 UTC, Nathan M. Swan wrote:
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