Generic swap: Difference between revisions
Content deleted Content added
→{{header|Pascal}}: add example |
Better D entry |
||
Line 253: | Line 253: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.algorithm: swap; // from Phobos standard library |
|||
⚫ | |||
⚫ | |||
void mySwap(T)(ref T left, ref T right) { |
|||
auto temp = left; |
|||
auto temp = left; |
|||
left = right; |
|||
right = temp; |
|||
} |
|||
void main() { |
|||
import std.stdio; |
|||
int[] a = [10, 20]; |
|||
writeln(a); |
|||
⚫ | |||
// contains a generic swap: |
|||
swap(a[0], a[1]); |
|||
writeln(a); |
|||
// Using mySwap: |
|||
mySwap(a[0], a[1]); |
|||
writeln(a); |
|||
}</lang> |
}</lang> |
||
{{out}} |
|||
⚫ | |||
<pre>[10, 20] |
|||
[20, 10] |
|||
[10, 20]</pre> |
|||
=={{header|dc}}== |
=={{header|dc}}== |