<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>