<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jun 7, 2016 at 2:39 PM, Tim Vermeulen <span dir="ltr">&lt;<a href="mailto:tvermeulen@me.com" target="_blank">tvermeulen@me.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">&gt; `while let element = sequence.next() where condition {…}`<br>
<br>
This is not a good alternative. First of all, it uses `while let` and `next()` instead of `for … in` which is way more complex than necessary. Also, if we want to skip over some elements, we either have to use<br>
<br>
`sequence.lazy.filter({ someCondition($0) }).next()`<br>
<br>
which is very hard to read compared to the proposal, or<br>
<br>
`guard someCondition(element) else { continue }`<br>
<br>
which is certainly better, but the added noise is still not very elegant. It’s also slightly confusing because we’d be both evaluating a condition outside of the loop body and inside the body.<br>
<br>
&gt; `for element in sequence.lazy.filter({ condition }) {…}`<br>
<br>
Not something I would go for either, again, because of the boilerplate code. You end up with an anonymous closure parameter despite already having named the element “element”, unless you name the parameter, but then you’d have to name it twice. Also, `.lazy.filter({ condition })` is a lot harder for beginners to grasp than a `where` or `while` keyword.<br>
<span class=""><br>
&gt; And it can be explicitly spelled out inside the loop<br>
<br>
</span>Probably the best option for now. In my opinion it’s still a bit confusing to enter the loop body but then decide to leave anyways, but it’s not too bad.<br></blockquote><div><br></div><div>I think it&#39;s a very good option, so we don&#39;t really disagree here :)</div><div><br></div><div>The bottom line is this: we have powerful methods on Sequence and Collection that allow you to iterate over conforming types without ever writing a loop. You&#39;re right that it becomes harder to read when you put them in a condition statement. But it&#39;s not just because there are dots and braces and dollar signs. Taken to the extreme, you could probably figure out a way to rewrite the whole body of a loop into the condition statement itself, leaving only empty braces. But that would be very silly and very unreadable even if we didn&#39;t use dots and braces and dollar signs.</div><div><br></div><div>Clearly, we both agree that a good way is to stick with how things were done in the old days, where things that are evaluated every iteration strictly inside the loop. Features like `where` clauses and `while` clauses provide new sugar that removes the punctuation (you call it &quot;noise&quot;) when using the same powerful methods on Sequence and Collection that we already have, thus *encouraging* more things to be put into the condition statement. Here, I disagree with you this would make for more readable code. As you and I agree, if the alternative to `sequence.lazy.filter {...}` is to put things inside the loop, we&#39;ll think twice about it and put things inside the loop. That, IMO, is where they best belong.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
&gt; You&#39;re describing a while loop:<br>
&gt; `while let element = sequence.next() where condition {...}`<br>
&gt;<br>
&gt; Which as we&#39;ve discussed can already be re-written with a for loop (which, yes, can be lazy):<br>
&gt; `for element in sequence.lazy.filter({ condition }) {...}`<br>
&gt;<br>
&gt; And it can be explicitly spelled out inside the loop, a definite readability gain for the same reason guard always requires an explicit else block.<br>
&gt;<br>
&gt; What do you gain with a new keyword?<br>
&gt;<br>
</span><span class="">&gt; On Tue, Jun 7, 2016 at 05:02 Haravikk via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>)&gt;wrote:<br>
&gt; &gt; I’m a +1 for this idea. Like Thorsten I was initially a little concerned that while and where may look too similar, but actually I find them visually distinct enough, and actually in my code I’m probably more likely to use while than where on for loops, although both are useful.<br>
&gt; &gt;<br>
</span><span class="">&gt; &gt; &gt;On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>)&gt;wrote:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;We can already use a where clause in a for loop like this:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;for element in array where someCondition(element) {<br>
&gt; &gt; &gt;// …<br>
&gt; &gt; &gt;}<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;which basically acts like<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;for element in array {<br>
&gt; &gt; &gt;guard someCondition(element) else { continue }<br>
&gt; &gt; &gt;// …<br>
&gt; &gt; &gt;}<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;Sometimes you want to break out of the loop when the condition isn’t met instead. I propose a while clause:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;for element in array while someCondition(element) {<br>
&gt; &gt; &gt;// …<br>
&gt; &gt; &gt;}<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;which would be syntactic sugar for<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;for element in array {<br>
&gt; &gt; &gt;guard someCondition(element) else { break }<br>
&gt; &gt; &gt;…<br>
&gt; &gt; &gt;}<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;I can see this particularly being useful if we have a sorted array and we already know that once the condition isn’t met, it won’t be met either for subsequent elements. Another use case could be an infinite sequence that we want to cut off somewhere (which is simply not possible using a where clause).<br>
&gt; &gt; &gt;_______________________________________________<br>
&gt; &gt; &gt;swift-evolution mailing list<br>
</span>&gt; &gt; &gt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>)<br>
<span class="">&gt; &gt; &gt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; swift-evolution mailing list<br>
</span>&gt; &gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>(mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>)<br>
&gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;<br>
&gt; </blockquote></div><br></div></div>