transversal sum

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 7 02:58:52 PST 2014


On Thursday, 6 November 2014 at 22:40:58 UTC, John Colvin wrote:
> this should be a textbook case for std.range.transposed, but I 
> can't seem to get it to work.

Ah, I didn't know this existed. Apparently it's not yet released, 
that's why it's not in the official documentation.

With DMD and Phobos from git:

     void main(string[] args)
     {
         import std.stdio: writeln;
         import std.range: transposed;
         import std.algorithm: map, sum;

         int[][] input = new int[][2];
         input[0] = [1, 2, 3, 4];
         input[1] = [5, 6, 7, 8];
         writeln(input);

         auto sums = input
                 .transposed
                 .map!(a => a.sum);
         writeln(sums);
     }

Output:

     [[1, 2, 3, 4], [5, 6, 7, 8]]
     [6, 8, 10, 12]


More information about the Digitalmars-d-learn mailing list