Matrix transposition: Difference between revisions

Updated both D entries
(→‎{{header|Pascal}}: add example)
(Updated both D entries)
Line 646:
 
=={{header|D}}==
===Locally Procedural Style===
<lang d>import std.stdio, std.algorithm, std.array, std.conv;
<lang d>import std.stdio;
 
autoT[][] transpose(T)(immutable /*in*/ T[][] m) pure nothrow {
auto r = new T[][]typeof(return)(m[0].length, m.length);
foreach (nr, row; m)
foreach (nc, c; row)
Line 661 ⟶ 662:
[18, 19, 20, 21]];
immutable T = transpose(M);
writelnwritefln("[", join%(map!text%(T%2d %), "\n %)"), "]"T);
}</lang>
{{out}}
Output:
<pre>[[10, 14, 18]
[11, 15, 19]
[12, 16, 20]
[13, 17, 21]]</pre>
===Functional style (same output):Style===
Same output.
<lang d>import std.stdio, std.algorithm, std.conv, std.range;
<lang d>import std.stdio, std.algorithm, std.array, std.convrange;
 
auto transpose(T)(in T[][] m) /*pure nothrow*/ {
return iota(m[0].length).map!(i => transversal(m, i))(iota(m[0].length));
}
 
Line 680 ⟶ 682:
[18, 19, 20, 21]];
/*immutable*/ auto T = transpose(M);
writelnwritefln("[", join%(map!text%(T%2d %), "\n %)"), "]"T);
}</lang>