monarch_dodra:

I'm taking this naming-changing event as an opportunity to "cleanup" reduce too.

This is possibly silly question, or perhaps fit just for D.learn. But I think it's sufficiently in topic in this thread.

You have a function foo1 like this:


import std.range, std.algorithm, std.array;

uint[] foo1(uint[][] X) {
    return X.reduce!((i, j) => zip(i, j)
                               .map!(kw => kw[0] | kw[1])
                               .array);
}

void main() {
    import std.stdio;
    uint[][] m1 = [[10, 20], [30, 40]];
    foo1(m1).writeln;
}


foo1 doesn't need to change its input so I'd like to use the signature:

int[] foo1(in int[][] X) {

But that can't work because reduce returns a const.

And you can't do this because it returns a wrong result:

uint[] foo2(in uint[][] X) {
    return reduce!((i, j) => zip(i, j)
                             .map!(kw => kw[0] | kw[1])
                             .array)
                  ((uint[]).init, X);
}



This seems OK, but can fold offer a better solution?:

uint[] foo3(in uint[][] X) {
    return reduce!((i, j) => zip(i, j)
                             .map!(kw => kw[0] | kw[1])
                             .array)
                  (X[0].dup, X[1 .. $]);
}

Bye,
bearophile

Reply via email to