Matrix transposition: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: add example) |
(Updated both D entries) |
||
Line 646: | Line 646: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Locally Procedural Style=== |
|||
⚫ | |||
<lang d>import std.stdio; |
|||
T[][] transpose(T)(immutable /*in*/ T[][] m) pure nothrow { |
|||
auto r = new |
auto r = new typeof(return)(m[0].length, m.length); |
||
foreach (nr, row; m) |
foreach (nr, row; m) |
||
foreach (nc, c; row) |
foreach (nc, c; row) |
||
Line 661: | Line 662: | ||
[18, 19, 20, 21]]; |
[18, 19, 20, 21]]; |
||
immutable T = transpose(M); |
immutable T = transpose(M); |
||
writefln("%(%(%2d %)\n%)", T); |
|||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre>10 14 18 |
||
11 15 19 |
|||
12 16 20 |
|||
13 17 21</pre> |
|||
Functional |
===Functional Style=== |
||
Same output. |
|||
⚫ | |||
⚫ | |||
auto transpose(T)(in T[][] m) /*pure nothrow*/ { |
auto transpose(T)(in T[][] m) /*pure nothrow*/ { |
||
return map!(i => transversal(m, i))( |
return iota(m[0].length).map!(i => transversal(m, i))(); |
||
} |
} |
||
Line 680: | Line 682: | ||
[18, 19, 20, 21]]; |
[18, 19, 20, 21]]; |
||
/*immutable*/ auto T = transpose(M); |
/*immutable*/ auto T = transpose(M); |
||
writefln("%(%(%2d %)\n%)", T); |
|||
}</lang> |
}</lang> |
||