Idea: mutatingForEach for Collections

This sounds like a useful addition! Note that Dictionary is troublesome as always — this method would need to be added in an extension to MutableCollection, but dictionaries themselves aren't mutable collections, only their Values sub-collections are. If you're okay with modifying the values independent of the keys, that's great, but if not, to solve your use case we might additionally need a modifyEachValueWithKey (I can come up with terrible names all day). That would look something like this:

extension Dictionary {
    mutating func modifyEachValueWithKey(
        _ body: (Key, inout Value) throws -> Void
    ) rethrows
}
5 Likes