swap - nodef/extra-lists GitHub Wiki

Exchange two values.

function swap(x, k, l)
// x: lists
// k: a key
// l: another key
const xlists = require('extra-lists');

var x = ['a', 'b', 'c', 'd'], [1, 2, 3, 4](/nodef/extra-lists/wiki/'a',-'b',-'c',-'d'],-[1,-2,-3,-4);
xlists.swap(x, 'a', 'b').map(c => [...c]);
// → [ [ 'b', 'a', 'c', 'd' ], [ 1, 2, 3, 4 ] ]

xlists.swap(x, 'a', 'd').map(c => [...c]);
// → [ [ 'd', 'b', 'c', 'a' ], [ 1, 2, 3, 4 ] ]

References