This is the most direct solution that preserves the behavior of my hypothetical version of removeAll(where:)
. The version that calls execute()
inline is good too, but fails if you are doing anything that requires you to use the array of removed objects as a whole. Things like:
let readyTasks = taskList.removeAll(where: { $0.isReady })
let alphabeticalTasks = readyTasks.sorted(by: { $0.name < $1.name })
alphabeticalTasks.forEach { $0.execute() }
In any case, the proposed alternatives aren't nearly as readable—doing consumptive work in the predicate supplied to removeAll
violates the principle of least astonishment, even in the case where the work is offloaded to a separate method/function/closure.