Matrix transposition: Difference between revisions

Updated D code
No edit summary
(Updated D code)
Line 656:
<lang d>import std.stdio, std.algorithm, std.array, std.conv;
 
pure nothrow auto transpose(T)(in T[][] m) pure nothrow {
auto r = new T[][](m[0].length, m.length);
foreach (nr, row; m)
Line 665:
 
void main() {
autoenum M = [[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]];
auto/*immutable*/ Mtconst T = transpose(M);
writeln("[", array(map!text(MtT)).join("\n "), "]");
}</lang>
Output:
Line 679:
<lang d>import std.stdio, std.algorithm, std.conv, std.range;
 
auto transpose(T)(in T[][] m) /*pure*/ {
return map!((int i){ return transversal(m,i); })(iota(m[0].length));
}
 
void main() {
auto enum M = [[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]];
/*immutable*/ auto MtT = transpose(M);
writeln("[", array(map!text(MtT)).join("\n "), "]");
}</lang>
 
Anonymous user