swiftlang/swift

[SR-3396] `reversed` for sequences and forward collections and should return a `ReversedRandomAccessCollection<[Iterator.Element]>`

Open

#45,984 opened on Dec 12, 2016

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
affects ABIgood first issueimprovementstandard libraryswift evolution proposal needed

Description

Previous ID SR-3396
Radar rdar://problem/21098134
Original Reporter @airspeedswift
Type Improvement

Attachment: Download

Votes 0
Component/s Standard Library
Labels Improvement, AffectsABI, StarterProposal
Assignee None
Priority Medium

md5: 34c0ebf025eb58406a81d44f15afed48

Issue Description:

Currently calling reversed() on a bi-directional and random-access collections returns a ReversedCollection and ReversedRandomAccessCollection respectively. This is because the view can be placed over the underlying collection at no cost – it's just a question of transforming the index appropriately and then accessing the underlying base collection.

For sequences and forward-only collections, however, this can't be done. So for these, the contents are read into an array and then that array is in-place reversed. But the second step is unnecessary – the array, once created, could just be returned wrapped in a ReversedRandomAccessCollection. As well as efficiency, this would have the benefit of making these methods more consistent across the board in what they return.

Contributor guide

[SR-3396] `reversed` for sequences and forward collections and should return a `ReversedRandomAccessCollection<[Iterator.Element]>` · swiftlang/swift#45984 | Good First Issue