[swift-evolution] Reduce with inout

Georgios Moschovitis george.moschovitis at icloud.com
Wed Jan 18 06:48:07 CST 2017


the former gives a hint about what someFunction (and the for-loop) is doing.

-g.

> On 18 Jan 2017, at 2:32 PM, Jeremy Pereira via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list