Hello. I have an array
[0, 1, 2, 3, 4, 5, 6, 7, 8]
. And I want to shift it to 4 elements right [0, 1, 2, 3, 4, 5, 6, 7, 8] * (-> 4)
to get [5, 6, 7, 8, 0, 1, 2, 3, 4]
. Does Standard Library suggest any tools to do it?
Tnx
I don't think there's anything in the Standard Library, but these pages may help you:
1 Like
There's also a sample implementation of a rotate algorithm in the Swift test suite.
2 Likes
Is this implemented in current version Xcode?
Thanks rotate(shiftingToStart: 4)
is desired method.
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8]
a.rotate(shiftingToStart: 4)
print(a) // [4, 5, 6, 7, 8, 0, 1, 2, 3]
.