Matrix transposition: Difference between revisions

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