Avatar
```swift var numbers = Array(1...15) // Find the indices of all the even numbers let indicesOfEvens = numbers.subranges(where: { $0.isMultiple(of: 2) }) // Perform an operation with just the even numbers let sumOfEvens = numbers[indicesOfEvens].reduce(0, +) // sumOfEvens == 56 ```
↑の numbers[indicesOfEvens] の結果のコレクションにおけるインデックスの扱いがどうなるんだろうと思ってたけど(他の言語みたいに array[range] のインデックスが 0 から始まらないのでどう整合性をとるのかなと)、↓こうなるのかぁ。
```swift extension Collection { /// Accesses a view of this collection with the elements at the given /// indices. /// /// - Complexity: O(1) public subscript(subranges: RangeSet<Index>) -> DiscontiguousSlice<Self> { get } } extension MutableCollection { /// Accesses a mutable view of this collection with the elements at the /// given indices. /// /// - Complexity: O(1) to access the elements, O(m) to mutate the /// elements at the positions in subranges, where m is the number of /// elements indicated by subranges. public subscript(subranges: RangeSet<Index>) -> DiscontiguousSlice<Self> { get set } } /// A collection wrapper that provides access to the elements of a collection, /// indexed by a set of indices. public struct DiscontiguousSlice<Base: Collection>: Collection { /// The collection that the indexed collection wraps. public var base: Base { get set } /// The set of index ranges that are available through this indexing /// collection. public var subranges: RangeSet<Base.Index> { get set } /// A position in an DiscontiguousSlice. struct Index: Comparable { // ... } public var startIndex: Index { get } public var endIndex: Index { set } public subscript(i: Index) -> Base.Element { get } public subscript(bounds: Range<Index>) -> Self { get } } ```
(edited)