<div dir="ltr">Any arguments against adding it? Otherwise I'll draft up a short proposal tomorrow.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 17, 2017 at 7:24 PM, Joe Groff via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
> On Jan 16, 2017, at 8:10 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
><br>
> on Mon Jan 16 2017, Charles Srstka <cocoadev-AT-charlessoft.com> wrote:<br>
><br>
>> On Jan 16, 2017, at 6:42 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>><br>
>>> on Mon Jan 16 2017, Charles Srstka <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>>> <mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.<wbr>org</a>>> wrote:<br>
>>><br>
>><br>
>>>>> On Jan 16, 2017, at 7:49 AM, Chris Eidhof via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>>>><br>
>>>>> Hi,<br>
>>>>><br>
>>>>> How does everyone feel about adding a second version of `reduce` to<br>
>>>><br>
>>>>> `Sequence`? Instead of a `combine` function that's of type `(A,<br>
>>>>> Element) -> A`, it would be `(inout A, Element) -> ()`. This way, we<br>
>>>>> can write nice functionals algorithms, but have the benefits of<br>
>>>>> inout (mutation within the function, and hopefully some copy<br>
>>>>> eliminations).<br>
>>>>><br>
>>>>> IIRC, Loïc Lecrenier first asked this on Twitter. I've been using it<br>
>>>>> ever since, because it can really improve readability (the possible<br>
>>>>> performance gain is nice, too).<br>
>>>>><br>
>>>>> Here's `reduce` with an `inout` parameter, including a sample:<br>
>>>>> <a href="https://gist.github.com/chriseidhof/fd3e9aa621569752d1b04230f92969d7" rel="noreferrer" target="_blank">https://gist.github.com/<wbr>chriseidhof/<wbr>fd3e9aa621569752d1b04230f92969<wbr>d7</a><br>
>>>>><br>
>>>>> --<br>
>>>>> Chris Eidhof<br>
>>>><br>
>>>> I did this in my own private code a while ago. There is one drawback, which is that Swift’s type<br>
>>>> inference system isn’t quite up to handling it. For example, doing this results in an “ambiguous<br>
>>>> reference to member” warning:<br>
>>>><br>
>>>> range.reduce([Int]()) { $0.append($1) }<br>
>>><br>
>>> The diagnostic could be better, but the compiler shouldn't let you do<br>
>>> that, because it requires passing an unnamed temporary value ([Int]())<br>
>>> as inout.<br>
>><br>
>> No it doesn’t. The signature of the method is:<br>
>><br>
>> func reduce<A>(_ initial: A, combine: (inout A, Iterator.Element) -> ()) -> A<br>
>><br>
>> The unnamed temporary value is “initial” here, which is not passed as inout; the inout parameter is<br>
>> the first argument to the “combine” closure. The value represented by the ‘initial’ parameter is<br>
>> passed to the closure, true, but only after being stored in a not-unnamed ‘var’ variable, as you can<br>
>> see from the source of the proposed method:<br>
>><br>
>> func reduce<A>(_ initial: A, combine: (inout A, Iterator.Element) -> ()) -> A {<br>
>> var result = initial<br>
>> for element in self {<br>
>> combine(&result, element)<br>
>> }<br>
>> return result<br>
>> }<br>
>><br>
>> Therefore, I don’t understand this objection.<br>
>><br>
>>>> One would think that the type of this closure should be clear:<br>
>>>><br>
>>>> 1) The closure calls append(), a mutating function, so $0 must be inout.<br>
>>>><br>
>>>> 2) The closure doesn’t return anything, which should rule out the<br>
>>>> default implementations of reduce,<br>
>>><br>
>>> The closure *does* return something: (), the empty tuple<br>
>><br>
>> But it’s not what it’s supposed to return. Sequence’s implementation<br>
>> of reduce, which the compiler thinks matches the above, is declared<br>
>> like this:<br>
>><br>
>> public func reduce<Result>(_ initialResult: Result, _<br>
>> nextPartialResult: (Result, Self.Iterator.Element) throws -> Result)<br>
>> rethrows -> Result<br>
>><br>
>> The closure is supposed to return Result, which in this case would be<br>
>> [Int]. It doesn’t, so I’m not sure why the compiler is thinking this<br>
>> is a match.<br>
><br>
> Okay, sounds like I'm totally wrong! Has to happen at least once in a<br>
> lifetime, doesn't it? 😉<br>
><br>
> So please file bug reports for these issues.<br>
<br>
</div></div>This one should already be fixed in master. If it isn't, definitely file a new one!<br>
<br>
-Joe<br>
<div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Chris Eidhof</div>
</div>