<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Not to doubt the usefulness of this proposal in general, but your example is subsumed by simply declaring </div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">var result = "\(gen.next()!)"</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">and proceeding with the rest of the example sans `once`. I think you'll also have to address how this is any different from dispatch_once or a DSL over the same.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Cheers,<br><br>~Robert Widmann</div><div><br>2016/05/17 0:03、Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> のメッセージ:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)">Hello Swift Evolution,</div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)">Its not uncommon to have to do a piece of work only once or on the first iteration of </div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)">a loop.</div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)">Take for example producing a comma separated string from an Array:</div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:'comic sans ms',sans-serif;color:rgb(39,78,19)">
<p class=""><span class=""> var</span><span class=""> result = </span><span class="">""</span></p><p class=""><span class=""> let</span><span class=""> values = [</span><span class="">1</span><span class="">, </span><span class="">2</span><span class="">, </span><span class="">3</span><span class="">, </span><span class="">4</span><span class="">, </span><span class="">5</span><span class="">]</span></p>
<p class=""><span class=""> var</span><span class=""> gen = </span><span class="">values</span><span class="">.</span><span class="">generate</span><span class="">()</span></p><p class=""><span class=""><br></span></p>
<p class=""><span class=""> if</span><span class=""> </span><span class="">let</span><span class=""> first = </span><span class="">gen</span><span class="">.</span><span class="">next</span><span class="">() {</span></p><p class=""><span class=""> result</span><span class=""> += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">first</span><span class="">)"</span></p><p class=""><span class=""> </span><span class="">while</span><span class=""> </span><span class="">let</span><span class=""> value = </span><span class="">gen</span><span class="">.</span><span class="">next</span><span class="">() {</span></p><p class=""><span class=""></span></p>
<p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">", "</span></p>
<p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">value</span><span class="">)"</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> }</span></p><p class=""><span class=""><br></span></p><p class=""><span class="">Since on the first iteration we want to skip putting a comma in front we use an `if let` </span>to grab the first element and then embed a `while let` inside the `if let` to handle the rest.</p><p class=""><br></p><p class="">Another way to do this could be using a bool to keep track of the first iteration:</p><p class=""><br></p><p class=""><span class=""> var</span><span class=""> first = </span><span class="">true</span></p><p class=""><span class=""> while</span><span class=""> </span><span class="">let</span><span class=""> value = </span><span class="">gen</span><span class="">.</span><span class="">next</span><span class="">() {</span></p><p class=""><span class=""> </span><span class="">if</span><span class=""> </span><span class="">first</span><span class=""> {</span></p><p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">value</span><span class="">)"</span></p><p class=""><span class=""> </span><span class="">first</span><span class=""> = </span><span class="">false</span></p><p class=""><span class=""> </span><span class="">continue</span></p><p class=""><span class=""> } </span><span class="">else</span><span class=""> {</span></p><p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">", "</span></p><p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">value</span><span class="">)"</span></p><p class=""><span class=""> }</span></p><p class="">
</p><p class=""><span class=""> }</span></p><p class=""><span class=""><br></span></p><p class=""><span class="">These approaches work, but I think there may be a way to do this with less noise.</span></p><p class="">If there was a keyword to execute a block of code only on the first iteration of a loop I think that would make code like this more concise.</p><p class="">If there was a keyword like `once` then the same thing could be achieved with something like:</p><p class=""><span class=""></span><br></p><p class=""><span class=""> while</span><span class=""> </span><span class="">let</span><span class=""> value = </span><span class="">gen</span><span class="">.</span><span class="">next</span><span class="">() {</span></p><p class=""><span class=""> once {</span></p><p class=""><span class=""> result += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">value</span><span class="">)"</span></p><p class=""><span class=""> </span><span class="">continue</span></p><p class=""><span class=""> }</span></p><p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">", "</span></p><p class=""><span class=""> </span><span class="">result</span><span class=""> += </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">value</span><span class="">)"</span></p><p class="">
</p><p class=""><span class=""> }</span></p><p class=""><br></p><p class="">How does it sound?</p><p class=""><br></p><p class="">- Nick</p></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>