apple / apple/swift-algorithms
Partition: Make mutating functions return `@discardableResult`
- Dominant language
- Swift
- Stars
- 6.3k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
Using the mutating partitioning functions are useful even when the returned index isn’t used.
The [Embracing Algorithms (WWDC 2018)](https://developer.apple.com/videos/play/wwdc2018/223/) session implements a `bringForward(elementsSatisfying:)` function on `MutableCollection`.1 It uses `stablePartition(by:)` in its implementation, but doesn’t need its return value, resulting in a warning.
### Actual behavior
```swift
extension MutableCollection {
mutating func bringForward(elementsSatisfying predicate: (Element) -> Bool) {
if let predecessor = indexBeforeFirst(where: predicate) {
self[predecessor...].stablePartition(by: { !predicate($0) }) // ⚠️ Result of call to 'stablePartition(by:)' is unused
}
}
}
```
### Expected behavior
```swift
extension MutableCollection {
mutating func bringForward(elementsSatisfying predicate: (Element) -> Bool) {
if let predecessor = indexBeforeFirst(where: predicate) {
self[predecessor...].stablePartition(by: { !predicate($0) })
}
}
}
```
While it could be argued that `bringForward(elementsSatisfying:)` should return an `Index` as well, even that return value isn’t always needed to be used and should be marked as `@discardableResult`.
### Checklist
- [x] If possible, I've reproduced the issue using the `main` branch of this package
- [x] I've searched for [existing GitHub issues](https://github.com/apple/swift-algorithms/issues)
----
1. This implementation function can be seen on page 218 of the presentation slides PDF.
Contributor guide
Research direction
Start by locating the mutating partitioning entry points, especially stablePartition(by:), in the Swift Algorithms source. Review their return-value declarations and related tests, then verify that callers such as bringForward(elementsSatisfying:) no longer produce an unused-result warning while callers that use the index still work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- data
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100