[swift-evolution] [Review] SE-0078: Implement a rotate algorithm, equivalent to std::rotate() in C++

Dmitri Gribenko gribozavr at gmail.com
Fri May 6 02:59:33 CDT 2016


On Fri, May 6, 2016 at 12:53 AM, Nate Cook <natecook at gmail.com> wrote:
> That brings up the question of which protocol to add the requirement to?
> Without a MutableBidirectionalProtocol (which we don't want, right?), we'd
> need to add it to MutableCollection. While a mutating reverse() is possible
> for a forward collection, it has significant space complexity, since it
> works either by creating an array of the indices or through recursion. We
> would also have reverse() available on some collections that don't have
> reversed(). Does that sound alright?

Good question!  I don't think we should provide reverse() on forward
collections.

I think we can play some tricks here.  We can add an underscored
_customReverse() requirement to MutableCollection, a default
implementation that traps, and two specialized implementations for
forward and bidirectional collections.  We would then add a protocol
extension (not a requirement) reverse() on MutableCollection where
Self : BidirectionalCollection that will call _customReverse().

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list