RosettaCodeData/Task/Matrix-transposition/D/matrix-transposition-2.d

17 lines
432 B
D
Raw Permalink Normal View History

2014-01-17 05:32:22 +00:00
T[][] transpose(T)(in T[][] m) pure nothrow {
auto r = new typeof(return)(m[0].length, m.length);
foreach (immutable nr, const row; m)
foreach (immutable nc, immutable c; row)
r[nc][nr] = c;
return r;
2013-04-10 21:29:02 -07:00
}
void main() {
2014-01-17 05:32:22 +00:00
import std.stdio;
2013-10-27 22:24:23 +00:00
immutable M = [[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]];
2014-01-17 05:32:22 +00:00
writefln("%(%(%2d %)\n%)", M.transpose);
2013-04-10 21:29:02 -07:00
}