Using the mutating partitioning functions are useful even when the returned index isn’t used.
The Embracing Algorithms (WWDC 2018) 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
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
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
- This implementation function can be seen on page 218 of the presentation slides PDF.
Using the mutating partitioning functions are useful even when the returned index isn’t used.
The Embracing Algorithms (WWDC 2018) session implements a
bringForward(elementsSatisfying:)function onMutableCollection.1 It usesstablePartition(by:)in its implementation, but doesn’t need its return value, resulting in a warning.Actual behavior
Expected behavior
While it could be argued that
bringForward(elementsSatisfying:)should return anIndexas well, even that return value isn’t always needed to be used and should be marked as@discardableResult.Checklist
mainbranch of this package