<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 24, 2017, at 1:27 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">Hmm, brainstorming here. Given the pervasive use of `with` to mean "this isn't accessible otherwise but inside this closure it's $0", maybe `reduce(with: 42) { $0 += 1 }` might give a useful hint?<br class=""></div></blockquote><div><br class=""></div><div>Are there any current uses of `with` passed `inout`?</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Jan 24, 2017 at 13:19 Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Jan 24, 2017, at 1:01 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="gmail_msg" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="gmail_msg m_-610305470151524434Apple-interchange-newline"><div class="gmail_msg">Hmm, it reads well, but IMO it avoids being misleading only because it doesn't mean anything. In what way are you reducing "into" the first argument any more so than the classic reduce function?</div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">It isn't perfect, but it’s better than the alternatives I’ve seen so far.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">In the classic reduce function a new value is produced for each step of the reduction.&nbsp; In this variation, each step reduces “into” an accumulator that is initialized with the seed value.&nbsp; In that sense, you could say that you reduce “into” the seed value.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Labeling the argument `into` is a little bit of a stretch but I think it's far superior to `mutating` which is pretty misleading at the call site.&nbsp; I think it would be pretty hard to come up with something better, but let’s keep the bikeshed going if anyone has additional ideas.</div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">On Tue, Jan 24, 2017 at 12:44 Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Jan 24, 2017, at 12:36 PM, Pyry Jahkola via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="gmail_msg m_-610305470151524434m_4877748158606825571Apple-interchange-newline"><div class="gmail_msg"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><span class="gmail_msg"></span></div><div class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Freak Show wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">Am I the only one who finds this incredibly ugly and hard to read?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">This is more or less solved by inject:into: idiom.&nbsp; There is no reason for inout for this particular problem.</div></div></blockquote><br class="gmail_msg"><div class="gmail_msg">Yeah, the original signature seems more useful. If you go all `inout` like Gwendal suggested, you might as well just iterate over the sequence with `for x in xs`, updating the state as you go.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">But your comment brought another idea to mind: if `mutating:` is considered a bad name for a non-`inout` argument, how about `<b class="gmail_msg">reduce(into:combine:)`</b>, similar to what Karl suggested earlier in this thread?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I think it reads very well at the call site, does not suggest `inout`ness of the argument too much (of course there's no `&amp;` at the call site either), and it's still easily found with auto-completion:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">&nbsp; &nbsp; let counts = words.reduce(into: [:]) {</div></div><div class="gmail_msg">&nbsp; &nbsp; &nbsp; $0[$1] = ($0[$1] ?? 0) + 1</div><div class="gmail_msg">&nbsp; &nbsp; }</div></div></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">+1.&nbsp; This is concise and I think it captures the essence of what is happening pretty well!</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">The third variation where the seed argument actually *is* `inout` might also be interesting in some cases where you *already* have a `var` that you want to accumulate into.&nbsp; I believe I have done this in the past in my own code but don’t have an example handy.</div></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">— Pyry</div></div>_______________________________________________<br class="gmail_msg">swift-evolution mailing list<br class="gmail_msg"><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"></div></blockquote></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>
</div></blockquote></div><br class="gmail_msg"></div></blockquote></div>
</div></blockquote></div><br class=""></body></html>