Matrix transposition: Difference between revisions
Content added Content deleted
(→{{header|Go}}: added version with flat representation) |
(Updated D code) |
||
Line 655: | Line 655: | ||
<lang d>import std.stdio, std.algorithm, std.array, std.conv; |
<lang d>import std.stdio, std.algorithm, std.array, std.conv; |
||
auto transpose(T)(in T[][] m) pure nothrow { |
auto transpose(T)(immutable /*in*/ T[][] m) pure nothrow { |
||
auto r = new T[][](m[0].length, m.length); |
auto r = new T[][](m[0].length, m.length); |
||
foreach (nr, row; m) |
foreach (nr, row; m) |
||
Line 664: | Line 664: | ||
void main() { |
void main() { |
||
immutable M = [[10, 11, 12, 13], |
|||
[14, 15, 16, 17], |
[14, 15, 16, 17], |
||
[18, 19, 20, 21]]; |
[18, 19, 20, 21]]; |
||
immutable T = transpose(M); |
|||
writeln("[", |
writeln("[", join(map!text(T), "\n "), "]"); |
||
}</lang> |
}</lang> |
||
Output: |
Output: |
||
Line 678: | Line 678: | ||
<lang d>import std.stdio, std.algorithm, std.conv, std.range; |
<lang d>import std.stdio, std.algorithm, std.conv, std.range; |
||
auto transpose(T)(in T[][] m) /*pure*/ { |
auto transpose(T)(in T[][] m) /*pure nothrow*/ { |
||
return map! |
return map!(i => transversal(m, i))(iota(m[0].length)); |
||
} |
} |
||
Line 687: | Line 687: | ||
[18, 19, 20, 21]]; |
[18, 19, 20, 21]]; |
||
/*immutable*/ auto T = transpose(M); |
/*immutable*/ auto T = transpose(M); |
||
writeln("[", |
writeln("[", join(map!text(T), "\n "), "]"); |
||
}</lang> |
}</lang> |
||