[swift-evolution] repeat loop enhancement
Daniel Steinberg
daniel at dimsumthinking.com
Wed Dec 9 07:30:03 CST 2015
Thanks Kenny
It’s so difficult for us to remember when these constructs weren’t familiar to us - to remember how much complexity there is in this code:
>
> for x in 1...5 {
> … some code…
> }
for x in 1…5 - what does 1 … 5 mean? What does it mean for x to be in 1 … 5? Now you tell me x isn’t in 1 … 5 and I can replace x with _ what does it mean for _ to be in 1 … 5 and why do I need _ at all? _ isn’t in the loop either. What’s a loop?
That’s not to say I haven’t taught this - I’m saying it’s a lot harder for someone new to programming to grasp than many people think.
I understand that it’s not a goal of the language to introduce syntax we only use in passing until they are ready to understand the for syntax so I won’t continue to argue the point. I was just voicing my support for the proposal and adding a use case where it might apply.
Best,
Daniel
>
> If you’re teaching to newcomers:
>
> - there’s no need to explain that counting starts at 0 because it doesn’t
> - if “x” is not used within the loop, then they will get a warning. That would be the perfect time to explain that they can just replace index with “_” when you don’t need to use the value within the loop.
>
> BTW, I hate the “in x ..< count” syntax. If you mean count from 0 to 4, then just write 0…4
>
> -Kenny
>
>
> Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks.
>
>> On Dec 8, 2015, at 1:54 PM, 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
More information about the swift-evolution
mailing list