GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Add Collection Operations on Noncontiguous Elements

Available from Swift 6.0

Paul Hudson      @twostraws

SE-0270 introduces various new methods to handle more complex operations on collections, such as moving or remove multiple items that aren't contiguous.

This change is powered by a new type called RangeSet. If you've ever used IndexSet from Foundation, think of RangeSet as being IndexSet except for any kind of Comparable type rather than just integers.

Lots of Swift API has been upgraded to RangeSet. To give us some example data to work with, we could create an array of students with exam results like this:

struct ExamResult {
    var student: String
    var score: Int
}

let results = [
    ExamResult(student: "Eric Effiong", score: 95),
    ExamResult(student: "Maeve Wiley", score: 70),
    ExamResult(student: "Otis Milburn", score: 100)
]

We can get a RangeSet containing the indices of all students who score 85% or higher like this:

let topResults = results.indices { student in
    student.score >= 85
}

And if we wanted to get access to those students, we can use a new Collection subscript:

for result in results[topResults] {
    print("\(result.student) scored \(result.score)%")
}

This subscript returns another new type called DiscontiguousSlice, which is similar to Slice in that for performance reasons it refers to elements stored in a different collection, except the indices are discontiguous, meaning that they aren't necessarily adjacent in the collection.

The "set" part of the name is there because RangeSet supports a variety of functions that come from the SetAlgebra protocol, including union(), intersection(), and isSuperset(of:). This also means that inserting one range into another will merge any overlapping ranges rather than creating duplicates.

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

Other changes in Swift 6.0…

Download all Swift 6.0 changes as a playground Link to Swift 6.0 changes

Browse changes in all Swift versions

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.