# Idea: mutatingForEach for Collections

**URL:** <https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442>\
**Category:** Discussion\
**Created:** [November 30, 2018, 3:36pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442 "2018-11-30T15:36:45Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Charles\_Constant](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charles_constant/32/13103_2.png) [@Charles\_Constant](https://forums.swift.org/u/Charles_Constant)\
**Post date:** [December 7, 2018, 2:46pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/42 "2018-12-07T14:46:45Z")

</div>

My vote for the name is ".mutateEach" since that is what it does. More importantly, "mutate" is one of the first words liable to pop into the programmers head when he or she needs to use it (we all type the word "mutate" repeatedly in this language)

The name is not a big deal, but Mutate, in this language, is more memorable than Modify

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 4:57pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/43 "2018-12-07T16:57:26Z")

</div>

> [@jawbroken](#):
>
> To add to what @Nevin wrote, I want to push back on this idea that this is a mutating version of `forEach` . This is a really more a mutating version of `map` (it's not a perfect match because the return type of map is `Array` for various practical reasons, the transform is inout instead of `Element -> T` ), and is only really related to `forEach` in the same loose fashion that any method that needs to loop over all the elements of a `Sequence` / `Collection` is related to `forEach` .

If you believe it’s a mutating version of `map`, then the guidelines very much apply and we should name it `formMap`!

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [December 7, 2018, 5:03pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/44 "2018-12-07T17:03:41Z")

</div>

`map` is a verb in this context, not a noun. It parallels `filter`.

(I'm splitting hairs, but we're discussing names, so everything is splitting hairs. I already mentioned why the signature is wrong for being a map rather than a forEach.)

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [December 7, 2018, 5:04pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/45 "2018-12-07T17:04:37Z")

</div>

`map` is not functioning as a noun here, so `formMap` would not adhere to the naming guidelines. I guess they would tell us that we should have had `map` (mutating) and `mapped` (non-mutating), but that would be a huge violation of the "stick to the established meaning if you do use a term of art" rule, so we rightfully didn't do that.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [December 7, 2018, 5:06pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/46 "2018-12-07T17:06:52Z")

</div>

As one of the two proposal authors, I'd like to politely request that we put a pin in the discussions around the naming guidelines. They dominated the previous thread, and while I like the game of linguistics as much as the next person, I'd also like to try to actually get stuff done.

I'm happy to hear name proposals, but at this stage I should stress that unless someone comes up with a really fantastic name I'll lean towards sticking with `modifyEach`: I don't think I've heard a proposal that is drastically better. Assuming we get to the proposal stage, I'll make it clear to the Swift Core Team that they should feel free to modify the name to something that fits the appropriate naming sensibilities of the standard library.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 5:10pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/47 "2018-12-07T17:10:34Z")

</div>

> [@Nevin](#):
>
> Here’s what the [API naming guidelines](https://swift.org/documentation/api-design-guidelines/) actually say about the `form` prefix:
> 
> API Naming Guidlines:
> 
> > When the operation is **naturally described by a noun** , use the noun for the nonmutating method and apply the “form” prefix to name its mutating counterpart.
> 
> The existing “ `forEach` ” is not a noun, so the “ `form` ” rule does not apply.

There is no need to parse this like a lawyer. The spirit and intent of the guidelines is to offer a way to create verb/noun pairs when the “noun” (the nonmutating operation) isn’t something that can be made into a verb in a natural way.

Obviously, since the word “form” generally precedes a noun in grammatical English, it can usually only be used with a noun. However “form for each” is very much grammatical and there is no reason why this rule can’t be applicable.

The guidelines don’t cover precisely every situation in the most literal sense—they’re not laws: they’re meant to _guide_.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 5:31pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/48 "2018-12-07T17:31:11Z")

</div>

> [@xwu](#):
>
> “form for each” is very much grammatical

It is not. 'nuff said. The relation between `forEach` and the new `modifyEach` is not even close to that between (e.g.) `union` and `formUnion`, where `formUnion` creates a union, and `union` mutates its input into the aforementioned union of sets.  
`forEach` mutates nothing, neither does it create nothing. It only provides input for side-effects. How can `forEach` fit with the `formSomething` guideline? It cannot.

(yes, there's a need to parse it like a lawyer when one feels it's the only way to make it stick.)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 6:02pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/49 "2018-12-07T18:02:47Z")

</div>

> [@glessard](#):
>
> > “form for each” is very much grammatical
> 
> It is not. 'nuff said.

Huh?

> [@glessard](#):
>
> The relation between `forEach` and the new `modifyEach` is not even close to that between (e.g.) `union` and `formUnion` , where `formUnion` creates a union, and `union` mutates its input into the aforementioned union of sets.  
> `forEach` mutates nothing, neither does it create nothing. It only provides input for side-effects. How can `forEach` fit with the `formSomething` guideline? It cannot.
> 
> (yes, there's a need to parse it like a lawyer when one feels it's the only way to make it stick.)

The point I'm trying to make is that, after an extensive discussion, Swift already has a word to describe "perform this operation, but in place." Alternatives such as "in place," "emplace," "mutate," "modify" were all considered but rejected in favor of the word "form."

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 6:44pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/50 "2018-12-07T18:44:18Z")

</div>

> [@xwu](#):
>
> Huh?

"to form" is a transitive verb for any sense that can be used here. "transitive" means it has a direct object. Where is the direct object? Your hypothetical "form for each" phrase has no direct object. What are we forming? Let's look at the closure: `(inout Type) -> Void`. Are we forming a void? Obviously not.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 7:07pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/51 "2018-12-07T19:07:33Z")

</div>

> [@xwu](#):
>
> The point I'm trying to make is that, after an extensive discussion, Swift already has a word to describe "perform this operation, but in place."

I believe there's also a notion of not making up nonsensical names, is there not?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 7:10pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/52 "2018-12-07T19:10:16Z")

</div>

> [@glessard](#):
>
> "to form" is a transitive verb for any sense that can be used here. "transitive" means it has a direct object. Where is the direct object? Your hypothetical "form for each" phrase has no direct object. What are we forming? Let's look at the closure: `(inout Type) -> Void` . Are we forming a void? Obviously not.

The beauty of the English word "form" is that it can be used without an object, in the sense of molding or modifying the subject.

That said, there's always the option of `formEach`, as I suggested above, if you wish to use "form" only as a transitive verb when it comes to Swift.

> [@glessard](#):
>
> I believe there's also a notion of not making up nonsensical names, is there not?

Still not sure why you think it's nonsensical.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 8:13pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/53 "2018-12-07T20:13:26Z")

</div>

> [@xwu](#):
>
> The beauty of the English word "form" is that it can be used without an object.

The intransitive uses do not apply here. When you don't have an explicit object there must be an implied one; the question remains: where is the object. I think you're fighting grammar here, and I don't see how it will yield. Perhaps you should form full sentences that illustrate just how well it works; I can't think of anything. What are you forming? Anything specific?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 8:20pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/54 "2018-12-07T20:20:16Z")

</div>

> [@glessard](#):
>
> The intransitive uses do not apply here. When you don't have an explicit object there must be an implied one; the question remains: where is the object.

Why must there be? Compare:

- result.get
- array.formForEach

And what I’m saying is precisely that the intransitive use _can_ apply here.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 8:25pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/55 "2018-12-07T20:25:13Z")

</div>

Why must there be an object? Because "to form" is a transitive verb which has a direct object, stated or implied. That statement is derived from English grammar.

"get the result" (explicitly stated) or throw (implied.)

"form for each array"? Either that doesn't mean what you think it means, or you didn't say what you actually meant. I'm thinking the latter.

I would remark that you keep falling back on repetition; I'm asking for new information. If you have none, then this can stop.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 8:38pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/56 "2018-12-07T20:38:11Z")

</div>

> [@glessard](#):
>
> "get the result" (explicitly stated) or throw (implied.)
> 
> "form for each array"? Either that doesn't mean what you think it means, or you didn't say what you actually meant. I'm thinking the latter.

- result.get(): get the result.
- array.formForEach { $0.formUnion(x) }: form the array, for each form union (with) x.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [December 7, 2018, 8:43pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/57 "2018-12-07T20:43:58Z")

</div>

For me, the number of words you needed to add clearly illustrates the opposite of the point you were trying to make. Thank you.

---

<div class="post-metadata">

**Author:** ![tkrajacic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkrajacic/32/1173_2.png) [@tkrajacic](https://forums.swift.org/u/tkrajacic)\
**Post date:** [December 7, 2018, 9:02pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/58 "2018-12-07T21:02:22Z")

</div>

We really need a better way to deal with naming discussions. This is completely drowning other arguments.

Maybe we can find a rule where any participant posts one comment with their suggestions (and possible short rationale), then compile a list of all suggestions and then move that to a different thread after all other issues have been discussed? (Just an idea. I am sure other people have better suggestions)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [December 7, 2018, 9:18pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/59 "2018-12-07T21:18:03Z")

</div>

To be fair, this is a proposal to add a single function for which the functionality is fairly uncontroversial and for which the naming is controversial.

This should be no surprise as the naming of the SetAlgebra methods was one of the most controversial threads of Swift Evolution in its first year.

While drawn out and difficult, I can’t see how one can design APIs without allowing debate over their names.

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [December 7, 2018, 9:50pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/60 "2018-12-07T21:50:06Z")

</div>

It seems possible to overload `forEach` so that the mutating one is used when possible. This is probably a bad idea.

```swift
extension MutableCollection {
  mutating func forEach(_ body: (inout Element) throws -> Void) rethrows {
    var index = self.startIndex
    while index != self.endIndex {
        try body(&self[index])
        self.formIndex(after: &index)
    }
  }
}

let array = ["A", "B", "C"]
array.forEach { // `Sequence.forEach(_:)`
  print($0)
}

var mutableArray = array
mutableArray.forEach { // `MutableCollection.forEach(_:)`
  $0 = $0.lowercased()
}

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [December 7, 2018, 10:27pm UTC](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442/61 "2018-12-07T22:27:48Z")

</div>

> [@benrimmington](#):
>
> It seems possible to overload `forEach` so that the mutating one is used when possible. This is probably a bad idea.

I had thought of that earlier, and decided not to post it because I also imagine it’s probably a bad idea.

But now that both of us think it, I’m pretty sure two bads make a good, right? :-)

[Previous page](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442.md?page=2)

[Next page](https://forums.swift.org/t/idea-mutatingforeach-for-collections/18442.md?page=4)
