# A non in-place version of rotate?

**URL:** <https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028>\
**Category:** Algorithms\
**Created:** [October 9, 2020, 12:07am UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028 "2020-10-09T00:07:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [October 9, 2020, 12:08am UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/1 "2020-10-09T00:08:00Z")

</div>

Hi,  
Looking into the rotate function and noted that there is not a `rotated` version that returns a copy instead of rotating in-place or even a lazy view version like e.g. [RotatedCollection](https://github.com/LucianoPAlmeida/Collections/blob/master/Collections/RotatedCollection.swift).

Just curious if this was a specific design decision to not include something like this and if it was, what are the downsides of that?

cc @nnnnnnnn

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [October 9, 2020, 3:05pm UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/2 "2020-10-09T15:05:18Z")

</div>

Not specifically an exclusion! The mutating version is a building block for some of other algorithms, so that's why it was included in the initial release.

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [October 9, 2020, 4:03pm UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/3 "2020-10-09T16:03:36Z")

</div>

The non-in-place version is also trivially composable.

edit: unless you want it to be a lazy wrapper I guess.

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [October 9, 2020, 4:16pm UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/4 "2020-10-09T16:16:49Z")

</div>

The lazy version is composable by chaining two slices together:

```swift
let c = [10, 20, 30, 40, 50]
let p = 2
let rotated = c[p...].chained(with: c[..<p])

```

However, a custom `Rotated` wrapper type can support (1) providing the starting index of the original collection and (2) handling re-rotation without slicing / chaining again.

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [October 10, 2020, 4:57am UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/5 "2020-10-10T04:57:51Z")

</div>

Thanks for the answers @nnnnnnnn @ben-cohen, to me it was more curiosity based on the fact that stdlib defines something like that for reversing a (reverse/reversed) method.

> [@nnnnnnnn](#):
>
> However, a custom `Rotated` wrapper type can support (1) providing the starting index of the original collection and (2) handling re-rotation without slicing / chaining again.

I really like the idea of a custom RotatedCollection wrapper! And since it is also composable, I think we would have nice benefits by having something like it in the library :)

---

<div class="post-metadata">

**Author:** ![kylemacomber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kylemacomber/32/12298_2.png) [@kylemacomber](https://forums.swift.org/u/kylemacomber)\
**Post date:** [October 11, 2020, 3:11pm UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/6 "2020-10-11T15:11:48Z")

</div>

I want to make sure that a lazy rotate wrapper is more than just a curiosity—is there a real-world use case where this would have come in handy?

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [October 11, 2020, 11:01pm UTC](https://forums.swift.org/t/a-non-in-place-version-of-rotate/41028/7 "2020-10-11T23:01:58Z")

</div>

I think not the lazy in fact but an implementation of a rotated method (even with copy) that returns a collection and allows us to chain calls with other functions e.g.

```
 let c = //... a collection
 c.rotated(by: 2)
  .dropFirst()
  .map(...) ... 

```

IMO the big advantage of a rotated wrapper, in this case, would be that as still composable, we gain the benefits of O(1) complexity on-call and also, avoiding a copy which can be a positive for large collections.

But as far as a real-world practical use case, TBH I can't think of one ...
