<div dir="ltr"><div class="gmail_extra">This can be done pretty easily, although I think the approach has the potential to cause confusion elsewhere in code. An obvious question is, should it be equivalent to 1...n, or 0..<n?</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">extension Int: SequenceType {</div><div class="gmail_extra"> public func generate() -> RangeGenerator<Int> {</div><div class="gmail_extra"> return (0..<self).generate()</div><div class="gmail_extra"> }</div><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">for i in 5 {</div><div class="gmail_extra"> print("hello \(i)")</div><div class="gmail_extra">}</div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob Bandes-Storch<br></div></div></div></div>
<br><div class="gmail_quote">On Fri, Dec 18, 2015 at 1:03 PM, Brent Royal-Gordon via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">> I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).<br>
<br>
</span>I’ve said it before, but I don’t think `times` is a good solution for learners. It teaches them a form of looping they will never use in practice and does not allow them to practice with ancillary loop skills like `break` and `continue`.<br>
<br>
I think our best bet is to extend the `for` loop to allow a single number, meaning either `1…n` or `0..<n` (you can argue it either way), and also to allow the `variableName in` part to be omitted, meaning `_ in`. This gives us the pedagogical simplicity of a “do this N times” loop, but couches it in a form where, when the student moves on, more commonly used loop forms are a straightforward extension of that simple case.<br>
<br>
for 5 { print(“Hello!”) }<br>
for i in 5 { print(“Hello \(i)!”) }<br>
for i in 10..<20 { print(“Hello \(i)!”) }<br>
for i in 10...20 { print(“Hello \(i)!”) }<br>
<span class=""><font color="#888888"><br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
</font></span><div class=""><div class="h5"><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div>