开发者

Pass a range of ranges to a variadic function

开发者 https://www.devze.com 2023-03-10 09:21 出处:网络
The Phobos documentation shows the following example of ranges passed to a variadic function int[] a = [ 1, 2, 4, 5, 7, 9 ];

The Phobos documentation shows the following example of ranges passed to a variadic function

int[] a = [ 1, 2, 4, 5, 7, 9 ];
int[] b = [ 0, 1, 2, 4, 7, 8 ];
int[] c = [ 0, 1, 4, 5, 7, 8 ];
assert(equal(setIntersection(a, a), a));
assert(equal(setIntersection(a, b), [1, 2, 4, 7][]));
assert(equal(setIntersection(a, b, c), [1, 4, 7][]));

But what if you have a range of ranges, and you don't know in advance how 开发者_高级运维many elements it will contain, like

int[][] a = [[1,2,3,4],[1,2,4,5],[1,3,4,5]];

The only thing I can think of is

if (a.length > 1) {
    auto res = array(setIntersection(a[0], a[1]));
    for (int i = 2; i < a.length; i++)
        res = array(setIntersection(res, a[i]));
    writeln(res);
}

Which works. But I was hoping to be able to pass the argument directly to the function, like setIntersection(a.tupleof) or something like that (I know that tupleof doesn't work here).


if you don't know how many elements a will have you won't be able to expand it into a tuple at compile time (and consequently pass it into a function)

so that for loop is your best bet (or implement your own setIntersection that can take a range of ranges)

0

精彩评论

暂无评论...
验证码 换一张
取 消