transversal sum

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 7 04:48:09 PST 2014


On Friday, 7 November 2014 at 10:58:58 UTC, Marc Schütz wrote:
> 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]

http://dlang.org/phobos-prerelease/std_range.html#.transposed

yeah it works find for arrays, but it needs assignable elements.


More information about the Digitalmars-d-learn mailing list