Idea: mutatingForEach for Collections

(Joe Groff) #62

Overloading can feel good in the spur of the moment, but only ever leads to long term pain.

(Ian Partridge) #63

I'm a bit surprised people don't think discovery via code completion and complementing forEach() is valuable. Ah well :slightly_smiling_face:


As a native English speaker, let me weigh in here. Your suggestion of using form... makes no sense to me. Neither formEach, nor formForEach, nor any other formulation. Whether it makes grammatical sense or not, it looks awkward, and it's a phrasing no English speaker I've ever interacted with would use.

(Xiaodi Wu) #65

Well, you may now consider yourself to have interacted with one such native English speaker who would use that phrasing :)

(Michel Fortin) #66

It'd be nice if there was a similar way to mutate dictionary values.

(Cory Benfield) #67

This will work for Dictionary values, as the collection returned by Dictionary.values conforms to MutableCollection.

(Jens Persson) #68

Has anyone suggested withEach?

mutableArray.withEach { $0 = $0.lowerCased() }


In fact, Dictionary.values is one of the places in the standard library where this proposal will be most beneficial, by avoiding the attractive nuisance of iterating over indices or keys (either of which would make an unnecessary copy of the dictionary when you mutate a value).


For what it's worth, in a past life of much reading and writing I earned a degree in English and American literature, and neither formForEach nor formMap make much sense to me alongside the other form-prefixed methods, nor do they read well. I think this is why you're getting what appears to be universal pushback here.