Skip to content

Instantly share code, notes, and snippets.

@Alfex4936
Last active November 8, 2020 03:22
Show Gist options
  • Save Alfex4936/d89fdc4c694a067c562f0992e2d2894e to your computer and use it in GitHub Desktop.
Save Alfex4936/d89fdc4c694a067c562f0992e2d2894e to your computer and use it in GitHub Desktop.
programming languages that support one-line swapping.
// Tuples
int a = 1;
int b = 2;
(a, b) = (b, a);
// vars
std::swap(x,y);
// iterable
std::iter_swap(std::begin(array) + i, std::begin(array) + i + 1);
# Crystal
A, B = A, B
// Go
A, B = A, B
A, B, C = B, A, C
# Julia
A, B = A, B
A, B, C = B, A, C
# https://nim-lang.org/docs/system.html#swap%2CT%2CT
swap(a, b)
# Python
A, B = A, B
A, B, C = B, A, C
// swap method defined for &mut [T]
// https://doc.rust-lang.org/std/primitive.slice.html#method.swap
mutable_element.swap(0, 2);
[array[j], array[j + 1]] = [array[j + 1], array[j]];
// V
A, B = A, B
A, B, C = B, A, C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment