nWayUnion(tuple)?

Ali Çehreli acehreli at yahoo.com
Wed Feb 27 13:13:38 PST 2013


On 02/27/2013 11:36 AM, bearophile wrote:

 > But in some cases I'd like to merge different types of ranges, that I
 > can't put in an array. Is this use case worth supporting (usually the
 > number of ranges is small so for such use cases a heap is not so needed)?

nWayUnion requires a range of ranges. The simplest thing to do for that 
is to pass an array. It is not very pretty, but to match the elements of 
that array, there is inputRangeObject:

import std.algorithm;
import std.range;
import std.stdio;

void main() {
     InputRange!int a = inputRangeObject(iota(10));
     InputRange!int b = inputRangeObject([3, 6, 9]);
     InputRange!int c = inputRangeObject(iota(11).map!q{a * a});
     auto r = nWayUnion([ a, b, c ]);

     writeln(r);
}

The output:

[0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 9, 16, 25, 36, 49, 64, 
81, 100]

Phobos could have a function so that the code could be cleaner. A 
function that supports "treat these as InputRanges of ints":

     nWayUnion(inputRanges(a, b, c));

But it is not there. :)

Ali



More information about the Digitalmars-d-learn mailing list