[swift-evolution] repeat loop enhancement
Daniel Steinberg
daniel at dimsumthinking.com
Tue Dec 8 18:43:23 CST 2015
For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)
I'm not ready to introduce for but repetition is a useful notion. I used to add repeat() in an extension to Int
5.repeat{ ...}
Is easier to describe than
for _ in 1...5 {...}
The _ and 1...5 are wonderful for us but often cause newbies to stumble
Now that repeat is a keyword I haven't come up with a good replacement yet
Daniel
> On Dec 8, 2015, at 6:21 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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:
>
> - retries before giving up on something
> - emitting padding when doing formatted output of some kind
> - reading formatted data when you have a given number of rows/records to read
>
> If it's that rare, I'm not sure it's worth dedicating syntax to.
> Jordan
>
>> On Dec 8, 2015, at 13:54, Jason Pollack via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> I'd like to propose a small enhancement to the repeat loop.
>>
>> Currently if we want to perform 'count' iterations in a loop, we need to do something like:
>>
>> for _ in 0 ..< count {
>> //Do something here
>> }
>>
>> 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.
>>
>> We can also do:
>>
>> var i = 0
>> repeat {
>> //Some code
>> i += 1
>> } while i < 10
>>
>> 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.
>>
>>
>> I propose the following:
>>
>> repeat count {
>> //Do something here
>> }
>>
>> It's cleaner, and IMO clearer what this code is intended to do.
>>
>> 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:
>>
>> var ok = true
>> repeat numberOfTimes {
>> //Do something, possibly set ok to false
>> } while ok
>>
>> 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).
>>
>> Thoughts?
>>
>> Thanks!
>> -Jason-
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151208/f98f446d/attachment.html>
More information about the swift-evolution
mailing list