After reading the problem, just on top of my head, this can be easily solved with a ring buffer. Best of all, it can be implemented in any language.
A good point, one of those if you knew the actual problem you'd find a way of solving it better than actually manipulating an array which feels "expensive".
Quick and really dirty and not exactly efficient :D
function rrotate(arr, steps) { steps = (steps || 0) % arr.length; return new Proxy(arr, { get(target, prop) { const idx = typeof prop === "string" ? Number(prop) - steps : NaN; return Number.isInteger(idx) ? target[idx >= 0 ? idx : target.length + idx] : target[prop]; } }); }
But it works (mostly)
Array.from(rrotate([1,2,3,4,5], 2)) // [4, 5, 1, 2, 3] rrotate([1,2,3,4,5], 2).slice() // [4, 5, 1, 2, 3] rrotate([1,2,3,4,5], 2).forEach(e => console.log(e)) // 4 // 5 // 1 // 2 // 3 [...rrotate([1,2,3,4,5], 7)] // [4, 5, 1, 2, 3] for (let e of rrotate([1,2,3,4,5], 12)) console.log(e); // 4 // 5 // 1 // 2 // 3
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
After reading the problem, just on top of my head, this can be easily solved with a ring buffer. Best of all, it can be implemented in any language.
A good point, one of those if you knew the actual problem you'd find a way of solving it better than actually manipulating an array which feels "expensive".
Quick and really dirty and not exactly efficient :D
But it works (mostly)