Skip to content

rotate$

Subhajit Sahu edited this page Nov 20, 2022 · 25 revisions

Rotate values in array.

Alternatives: rotate, rotate$.
Similar: repeat, cycle, rotate, reverse.


function rotate$(x, n)
// x: an array (updated)
// n: rotate amount (+ve: left, -ve: right) [0]
const array = require('extra-array');

var x = [1, 2, 3, 4];
array.rotate$(x, 1);
// → [ 2, 3, 4, 1 ]

x;
// → [ 2, 3, 4, 1 ]

var x = [1, 2, 3, 4];
array.rotate$(x, 2);
// → [ 3, 4, 1, 2 ]

var x = [1, 2, 3, 4];
array.rotate$(x, -1);
// → [ 4, 1, 2, 3 ]


References

Clone this wiki locally