<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 Jun 9, 2016, at 5:51 AM, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 8, 2016, at 9:36 PM, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" class="">Upon accepting SE-0099, the core team is removing `where` clauses from condition clauses, writing "the 'where' keyword can be retired from its purpose as a boolean condition introducer." <br class=""><br class="">Inspiried by Xiaodi Wu, I now propose removing `where` clauses from `for in` loops, where they are better expressed (and read) as guard conditions. <br class=""></blockquote><br class="">Do you propose to remove `for case` as well? That can equally be handled by a `guard case` in the loop body.<br class=""><br class="">Alternate proposal: Move `where` clauses to be adjacent to the pattern—rather than the sequence expression—in a `for` loop, just as they are in these other syntaxes.<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for n where n.isOdd in 1...1_000 { … }<br class=""><br class="">This makes them more consistent with the syntax in `switch` cases and `catch` statements, while also IMHO clarifying the role of the `where` clause as a filter on the elements seen by the loop.<br class=""></div></div></blockquote></div><br class=""><div class="">I saw your post on that *after* I finished sending this. Moving `where` next to the pattern, like you'd find in `catch` and switch `case`, the code would look like this:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">for i where i % 2 == 0 in sequence {</font></div><div class=""><font face="Menlo" class=""> // do stuff</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">I agree that's really clever and an improvement but after coming up with all the points about wrong expectations about termination vs filtering, the better use of guard, and fetishes about vertical compactness, I think (call it +0.6) I'm going to stick to my guns on this one - and for `for case` too. I've been wuxxed.</div><div class=""><br class=""></div><div class="">* New users might expect the sequence to terminate as soon as i % 2 is 1, rather than the correct interpretation which is "this is a filtering operation"</div><div class="">* The code can be expressed less ambiguously as </div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">for i in sequence.filter({ return i % 2 == 0 }) {</font></div><div class=""><font face="Menlo" class=""> // do stuff</font></div><div class=""><font face="Menlo" class="">}</font></div></div></div></blockquote><div><br class=""></div>It's important to keep in mind that .filter without using .lazy copies the array. So you need to keep using sequence.lazy.filter({ return i %2 == 0 }), unless you're OK with giving up some performance, which a) adds boilerplate, b) not many people will remember to do.</div><div><br class=""></div><div>I've taken the time to run a test, going through milion numbers (several times) using:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">arr</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> i % </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">2</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> == </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="color: rgb(187, 44, 162);" class="">continue </span>} }</div></div><div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">arr</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i % </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span><span style="font-variant-ligatures: no-common-ligatures" class=""> == </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0 </span>{ }</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">arr</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">filter</span><span style="font-variant-ligatures: no-common-ligatures" class="">({ $0 % </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span><span style="font-variant-ligatures: no-common-ligatures" class=""> == </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class=""> })</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class=""> </span>{ }</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">for</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">in</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">arr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">lazy</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);" class="">filter</span><span style="font-variant-ligatures: no-common-ligatures;" class="">({ $0 % </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">2</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> == </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> })</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class=""> </span>{ }</div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><div style="font-family: Helvetica; font-size: 10px;">Results:</div></div><div style="font-family: Helvetica; font-size: 10px;"><br class=""></div><div style="font-family: Helvetica; font-size: 10px;">- plain for loop with if-continue: 27.19 seconds (+1.76%)</div><div style="font-family: Helvetica; font-size: 10px;">- with where: 26.72 seconds (+0.00%)</div><div style="font-family: Helvetica; font-size: 10px;">- .filter: 44.73 seconds (+67.40%)</div><div style="font-family: Helvetica; font-size: 10px;">- .lazy.filter: 31.66 seconds (+18.48%)</div><div style="font-family: Helvetica; font-size: 10px;"><br class=""></div><div style="font-family: Helvetica; font-size: 10px;">Yes, 100 milion numbers is an extreme, but it demonstrates that any of the suggested expressions will be slower, mainly if the caller doesn't use .lazy (67% !!!). The only comparable solution is adding additional lines of code into the body of the for loop by adding an if statement.</div><div class=""><br class=""></div></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">* The while version can be expressed as</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">for i in sequence.prefix(while: { return $0 % 2 == 0 } ) {</font></div><div class=""><font face="Menlo" class=""> // do stuff</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">* The code can also use `guard` statements as needed with `break` and `continue`</div><div class=""><br class=""></div><div class=""><div class="">(And yes, I should have pointed out filter and prefix as well as guard in my first email)</div><div class=""><br class=""></div></div><div class="">-- E</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>