<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)</div><div><br></div><div>I'm not ready to introduce for but repetition is a useful notion. I used to add repeat() in an extension to Int</div><div><br></div><div>5.repeat{ ...}</div><div><br></div><div>Is easier to describe than </div><div><br></div><div>for _ in 1...5 {...}</div><div><br></div><div>The _ and 1...5 are wonderful for us but often cause newbies to stumble</div><div><br></div><div>Now that repeat is a keyword I haven't come up with a good replacement yet</div><div><br></div><div>Daniel</div><div><br>On Dec 8, 2015, at 6:21 PM, Jordan Rose via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div class="">I've also thought of this idea before, but how often in code do you actually repeat something a given number of times? There are only a few use cases I can think of:</div><div class=""><br class=""></div><div class="">- retries before giving up on something</div><div class="">- emitting padding when doing formatted output of some kind</div><div class="">- <i class="">reading</i> formatted data when you have a given number of rows/records to read</div><div class=""><br class=""></div><div class="">If it's that rare, I'm not sure it's worth dedicating syntax to.</div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 8, 2015, at 13:54, Jason Pollack 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=""><div dir="ltr" class="">I'd like to propose a small enhancement to the repeat loop.<div class=""><br class=""></div><div class="">Currently if we want to perform 'count' iterations in a loop, we need to do something like:</div><div class=""><br class=""></div><div class="">for _ in 0 ..< count {</div><div class=""> //Do something here</div><div class="">}</div><div class=""><br class=""></div><div class="">It looks and feels a little awkward. We need to create an unnamed variable, and it's easy to forget (especially for language newcomers) that the loop starts with 0 and doesn't include count.</div><div class=""><br class=""></div><div class="">We can also do:</div><div class=""><br class=""></div><div class="">var i = 0</div><div class="">repeat {</div><div class=""> //Some code</div><div class=""> i += 1</div><div class="">} while i < 10</div><div class=""><br class=""></div><div class="">This is worse, in that it introduces a variable in the outer scope, and puts the repeat count at the end. Plus, if the expression inside the repeat is complex, the value of i may never be incremented, or may be incremented more than once.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I propose the following:</div><div class=""><br class=""></div><div class="">repeat count {</div><div class=""> //Do something here</div><div class="">}</div><div class=""><br class=""></div><div class="">It's cleaner, and IMO clearer what this code is intended to do.</div><div class=""><br class=""></div><div class="">Naturally 'count' should be non-negative. A while clause is not needed, although I could imagine it being supplied, to create a construct such as:</div><div class=""><br class=""></div><div class="">var ok = true</div><div class="">repeat numberOfTimes {</div><div class=""> //Do something, possibly set ok to false</div><div class="">} while ok</div><div class=""><br class=""></div><div class="">This would repeat the loop a maximum of 'numberOfTimes', but could be ended early if some signal 'ok' is set to false (or, of course, by a break statement).</div><div class=""><br class=""></div><div class="">Thoughts?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">-Jason-</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=f6VunEbxOyO0-2FacKqVTeu6pGEEellWH1YmuP-2Bt5lLhNtmIXF-2BaM45P9puL1WYbUmrdej-2F87olnZt0sRLdwJH7jTeozr4Sr0qhjZK-2BXxD4gXfNAInwgHkMSsJhlAzB5UFrHmXSNox-2B3Sq4JVS0ZYe9IPUJGmWaO5KpLgCMl2E0j1W9fKfq9b5p7g7myqY8nrIo6WtXYXOZk2AsFgB0JxdU5A114lZcySuMS6Oa5-2B5A3c-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" 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">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=AdkfTiApI80cNEyortTzHSh85rkZBhnEguMJR2uT8lBdyUMNrdkS7-2BqlH-2BfOY9FewmyCIPe6OvYyUY4De59gSR5Q6DNf6ZX-2FQ9yRoduRPRs0cvoxWj5Yw7WxIFlrN4xbaRqbYQZZKVzWd9yM8mFamqclpDU3CpGNUYonYHVGrVXiPRGfOdTc1cmCVLqAAhr-2Fqt2lbp2ZzC996TLV185mEhtldwH4tHKApJNQMOtkQWg-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">
</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>