<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="">This is more a demonstration that the for...else construct isn't obvious than a solution to the problem. In Python, the else branch of a for loop is executed when you went through all the iterations without breaking ("when the loop condition becomes false", which doesn't happen if you break from the loop).<br class=""><div class="">
<br class="Apple-interchange-newline"><span style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;" class="">Félix</span>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 25 janv. 2016 à 23:14:23, Andrew Bennett via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This may do what you want:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">let</span> values: [<span style="color:rgb(112,61,170)" class="">Int</span>] = [<span style="color:rgb(39,42,216)" class="">1</span>,<span style="color:rgb(39,42,216)" class="">2</span>,<span style="color:rgb(39,42,216)" class="">3</span>,<span style="color:rgb(39,42,216)" class="">4</span>]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">if</span> <span style="color:rgb(187,44,162)" class="">let</span> sequence = <span style="color:rgb(49,89,93)" class="">nonEmptySequence</span>(<span style="color:rgb(79,129,135)" class="">values</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">for</span> element <span style="color:rgb(187,44,162)" class="">in</span> sequence {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(61,29,129)" class="">print</span>(<span style="color:rgb(209,47,27)" class="">"element: </span>\<span style="color:rgb(209,47,27)" class="">(</span>element<span style="color:rgb(209,47,27)" class="">)"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(61,29,129)" class="">print</span>(<span style="color:rgb(209,47,27)" class="">"empty!"</span>)</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
</p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div>It's also more flexible, you can do work after the for loop (that work won't execute in the else), you can also use PeekSequence directly to get the first value. This is handy for something like an average (reduce) where you don't necessarily have a zero value.</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div>The implementation can be found here: <a href="https://github.com/therealbnut/PeekSequence" class="">https://github.com/therealbnut/PeekSequence</a></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jan 26, 2016 at 12:59 AM, Matthew Johnson via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class=""><div class=""><br class=""><br class="">Sent from my iPad</div><span class=""><div class=""><br class="">On Jan 25, 2016, at 3:41 AM, Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""></div><div class="">It would be easy enough to add:</div><div class=""><br class=""></div><ul style="margin:0px;padding:0px;width:auto;list-style:none" class=""><li style="text-indent:0px;border-left-style:none;margin-left:4em;padding-bottom:0px!important" class=""><font face="UICTFontTextStyleTallBody" class=""><span style="background-color:rgba(255,255,255,0)" class=""><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">func</code> <code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">forEach</code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">(</code></span></font></li><li style="text-indent:0px;border-left-style:none;margin-left:4em;padding-bottom:0px!important" class=""><font face="UICTFontTextStyleTallBody" class=""><span style="background-color:rgba(255,255,255,0)" class=""><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class=""> @</code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">noescape</code> elements <code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">body</code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">: (</code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">Element</code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">) </code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">throws<font face="UICTFontTextStyleTallBody" class=""> -> Void</font></code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">,</code></span></font></li><li style="text-indent:0px;border-left-style:none;margin-left:4em;padding-bottom:0px!important" class=""><font face="UICTFontTextStyleTallBody" class=""><span style="background-color:rgba(255,255,255,0)" class=""><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class=""> @noescape otherwise otherwise: () throws -> Void</code></span></font></li><li style="text-indent:0px;border-left-style:none;margin-left:4em;padding-bottom:0px!important" class=""><font face="UICTFontTextStyleTallBody" class=""><span style="background-color:rgba(255,255,255,0)" class=""><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">) </code><code style="padding:0px!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;background-image:none!important;border:0px!important;float:none!important;min-height:auto!important;line-height:1.1em!important;margin:0px!important;outline:0px!important;overflow:visible!important;vertical-align:baseline!important;width:auto!important;min-height:inherit!important" class="">rethrows</code></span></font></li></ul><div class=""><font face="monospace" class=""><br class=""></font></div><div class=""><font face="monospace" class="">to the standard library or write it yourself.</font></div></div></blockquote><div class=""><br class=""></div></span><div class="">Not unless / until we get the ability for a noescape closure to influence control flow (break, return, etc). Even then this is clunkier than adding else to the language would be.</div><div class=""><div class="h5"><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">On 23 Jan 2016, at 12:59 PM, Félix Cloutier via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class="">I frequently need something like that and I'd like to have it in Swift too.<br class=""><div class="">
<br class=""><span style="font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; float: none; display: inline !important;" class="">Félix</span>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 22 janv. 2016 à 13:22:45, Ben Langmuir via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> a écrit :</div><br class=""><div class=""><div class="">Previous discussion here:<br class=""><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001110.html" target="_blank" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001110.html</a><br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jan 22, 2016, at 8:22 AM, Lee M via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">This has likely been discussed already, but I haven't been able to find it anywhere.<br class="">Example:<br class=""><br class=""> let items: Int = []<br class=""><br class=""> for item in items {<br class=""> print(item)<br class=""> }<br class=""> else {<br class=""> print("No items")<br class=""> }<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></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>