[swift-evolution] Reduce with inout

Jeremy Pereira jeremy.j.pereira at googlemail.com
Wed Jan 18 06:32:13 CST 2017

> On 18 Jan 2017, at 12:26, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> Thought: if the idea is performance and not drop-in replacement, why force the user to incur two copies? If the initial value were inout, this function would be more unambiguous even without a new name, and at _worst_ the user has to declare a variable with var, a worthwhile trade-off to save two copies.

That’s what I thought also until just now, but then why wouldn’t you just use a for …  in loop?

i.e. instead of

    var foo = 0
    let bar: [SomeType] = ...

    bar.reduce(mutating: &foo, someFunction) 

You would write

    var foo = 0
    let bar: [SomeType] = …

    for e in bar {
        someFunction(&foo, e)

which is a bit more readable IMO

More information about the swift-evolution mailing list