<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="">Aren’t there already methods that stick elements between others (and only between, neither before the first nor after the last)?<div class=""><br class=""></div><div class="">//=====</div><div class="">fun toCSV2(values: [CustomStringConvertible]) -> String {</div><div class=""> return values.map { $0.description }.joinWithSeparator(“, “)</div><div class="">}</div><div class="">//=====</div><div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">— </div><div class="">Daryle Walker<br class="">Mac, Internet, and Video Game Junkie<br class="">darylew AT mac DOT com </div></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On May 17, 2016, at 11:49 AM, Haravikk 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=""><div class="">This only works with the example exactly as-is, I think a fairer re-write would be something like the following:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> toCSV(values:[</span>CustomStringConvertible<span style="font-variant-ligatures: no-common-ligatures;" class="">]) -> </span>String <span style="font-variant-ligatures: no-common-ligatures;" class="">{</span></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> gen = values.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">generate</span>()</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> result = gen.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">next</span>()?.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">description</span> ?? <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> value = gen.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">next</span>() { result += <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">", </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>value<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span> }</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> result</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">toCSV</span>([<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span>])</div></div></blockquote><div style="margin: 0px;" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><br class=""></span></font></div><div style="margin: 0px;" class="">For brevity I opted to use nil-coalescing and a default value to handle the first (potentially nil) element cleanly.</div><div style="margin: 0px;" class=""><br class=""></div><div style="margin: 0px;" class="">I’m undecided about whether I want the feature or not; as shown the example can actually be handled very neatly already, so it doesn’t really highlight a need for the feature. One thing I like about it though is the ability to possibly eliminate the need for a generator and use a for in loop instead, like so:</div><div style="margin: 0px;" class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> toCSV(values:[</span>CustomStringConvertible<span style="font-variant-ligatures: no-common-ligatures;" class="">]) -> </span>String <span style="font-variant-ligatures: no-common-ligatures;" class="">{</span></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> result = <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> value <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> values {</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> once { result += <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>value<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span>; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">continue</span> }</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> result += <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">", </span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>value<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span></div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> }</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> result</div></div><div style="margin: 0px;" class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div></div></blockquote><div style="margin: 0px;" class=""><br class=""></div><div style="margin: 0px;" class="">But this isn’t really much of a saving. I can’t think of a real-world example offhand that can better demonstrate a use for this; ideally you’d want to use several once {} blocks, or do something in the loop that requires other statements in the loop, i.e- you use the once to avoid duplicating statements in the loop while processing the first element.</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 17 May 2016, at 07:13, Robert Widmann 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 dir="auto" class=""><div class="">Not to doubt the usefulness of this proposal in general, but your example is subsumed by simply declaring </div><div class=""><br class=""></div><div class="">var result = "\(gen.next()!)"</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Cheers,<br class=""><br class="">~Robert Widmann</div><div class=""><br class="">2016/05/17 0:03、Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> のメッセージ:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><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 class=""></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 class=""></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 class=""></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><div class=""><span class=""></span><br class="webkit-block-placeholder"></div><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 class=""></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 class=""></p><p class="">Another way to do this could be using a bool to keep track of the first iteration:</p><p class=""><br class=""></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><div class="">
<br class="webkit-block-placeholder"></div><p class=""><span class=""> }</span></p><p class=""><span class=""><br class=""></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 class=""></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><div class="">
<br class="webkit-block-placeholder"></div><p class=""><span class=""> }</span></p><p class=""><br class=""></p><p class="">How does it sound?</p><p class=""><br class=""></p><p class="">- Nick</p></div></div>
</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" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div>_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></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=""></div></body></html>