Repeat loop enhancement


(Jason Pollack) #1

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-


(Kevin Kachikian) #2

I like it!

Anything to keep heading down the cleaner syntax path as Swift cleans up so much of the visual noise that ObjC used to present.

That said, with. map and other capabilities, how often would this be used?

···

On Dec 8, 2015, at 1:54 PM, Jason Pollack via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Erica Sadun) #3

You can do:

(0 ..< count).forEach {
    // Do something here
}

You can also easily extend Int for a similar result:

public extension Int {
    func times(@noescape body: () -> Void) {(0..<self).forEach{_ in body()}}
}

-- E

···

On Dec 8, 2015, at 2:54 PM, Jason Pollack via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jordan Rose) #4

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #5

If the goal is to avoid leaking variables into the outer scope, you might
be happy with something like

repeat var x = 0 {
    // ...
    [update x]
} while [condition that has access to x]

// x is out of scope here

Jacob

···

On Tue, Dec 8, 2015 at 1:54 PM, Jason Pollack via swift-evolution < swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


#6

I propose the following:

repeat count {
    //Do something here
}

It's cleaner, and IMO clearer what this code is intended to do.

I'm not sure I like the idea of adding this functionality over "repeat". If
"for _ in" is ugly, there's always the opportunity to add a helper, e.g.:

    extension Int {
        func times(@autoclosure while condition: () -> Bool = true, body:
() -> Void) {
            // ...
        }
    }

    5.times {
        // ...
    }

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).

You can use a "where" clause to handle this in a for-in loop:

    var ok = true
    for _ in 0..<numberOfTimes where ok {
        // ...
    }

Stephen

···

On Tue, Dec 8, 2015 at 4:54 PM, Jason Pollack via swift-evolution < swift-evolution@swift.org> wrote:


(Daniel Steinberg) #7

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@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Kenny Leung) #8

Why don’t you just do

for x in 1...5 {
  … some code...
}

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Eli Hini) #9

+1

···

Sent from my iPhone

On Dec 8, 2015, at 21:10, Kenny Leung via swift-evolution <swift-evolution@swift.org> wrote:

Why don’t you just do

for x in 1...5 {
   … some code...
}

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Chris Lattner) #10

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations. There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris

···

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org> wrote:

For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)


(Daniel Steinberg) #11

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(André Videla) #12

How about having a default implementation in the standard library for it?
We avoid syntactic sugar and we allow for this very simple yet useful expression.
And we’ve seen solutions in this thread using autoclosurses and extensions

5.times {
   …
}

or a function

repeat(4) {
   …
}

all sound good to me.

- André

···

On 09 Dec 2015, at 06:58, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations. There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Liam Butler-Lawrence) #13

+1 for repeat X { } (as a keyword/statement, not a function).

It's definitely syntax sugar, but I agree it would give beginners an easy "in" to for loops. It would also make some of the code we write a bit smaller and more readable, which is always a good thing. I haven't seen any serious detriments put forward either.

Liam

···

Sent from my iPhone

On Dec 9, 2015, at 8:30 AM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org> wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Brent Royal-Gordon) #14

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?

You know, I look at this and wonder, why can’t you just say `for 1…5`?

···

--
Brent Royal-Gordon
Architechies


(thorsten@portableinnovations.de) #15

Why not define a times() or timesRepeat() method on Int taking a closure to execute multiple times, like Erica recently posted and which is common in other languages like Ruby, Smalltalk or (I think) Scala:

5.times { ... }

or

5.timesRepeat { ... }

That reads quite naturally and does not need any language extension.

-Thorsten

···

Am 09.12.2015 um 06:58 schrieb Chris Lattner via swift-evolution <swift-evolution@swift.org>:

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org> wrote:
For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations. There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Chris Lattner) #16

How about having a default implementation in the standard library for it?
We avoid syntactic sugar and we allow for this very simple yet useful expression.
And we’ve seen solutions in this thread using autoclosurses and extensions

5.times {
   …
}

If the teaching angle is the motivation, then I’d say that this is worse.

or a function

repeat(4) {
   …
}

Note that this *is* the proposal. :-) “repeat” is a keyword in swift.

-Chris

···

On Dec 8, 2015, at 10:05 PM, André Videla <zephyz@me.com> wrote:

all sound good to me.

- André

On 09 Dec 2015, at 06:58, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations. There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jason Pollack) #17

Solutions like this look reasonable, until you try to use normal control
flow statements as you would in other loops. In the closure, a break or
continue are illegal, and a return acts like a continue.

var ok = true

5.times {
    ok = someTest()

    if (!ok) {
        //break - Nope!
        return //Sure, but loop continues anyway
    }
}

Sure, it's a contrived example, but it's easy to see how this construct is
not like other loops.

On the other hand, it might be nice to be able to decorate a closure such
that break and continue behave as one might expect inside a loop, but that
sounds like a can of worms.

···

On Wed, Dec 9, 2015 at 1:05 AM, André Videla <zephyz@me.com> wrote:

How about having a default implementation in the standard library for it?
We avoid syntactic sugar and we allow for this very simple yet useful
expression.
And we’ve seen solutions in this thread using autoclosurses and extensions

5.times {
   …
}

or a function

repeat(4) {
   …
}

all sound good to me.

- André

On 09 Dec 2015, at 06:58, Chris Lattner via swift-evolution < > swift-evolution@swift.org> wrote:

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution < > swift-evolution@swift.org> wrote:

For me this comes up when teaching children or new programmers. (Perhaps
not a valid use case)

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2
implementation of the feature, but was cut due to schedule limitations.
There is precedent for this sort of feature in many teaching oriented
languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what
the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tyler Cloutier) #18

I’m still of the belief that all types of loops could be condensed down to a single type of loop, the repeat-for loop

Consider:

The for-in loop:

repeat for x in 0..<5 {
    // code
}

The C style for loop and the while loop:

repeat for var i = 0 while someCondition(i) {
    // code
}

The repeat while:

repeat for var i = 0 {
    // code
} while someConditionEvaluatedAfterFirstLoop() // The i variable would likely have to be considered in scope in the while expression

The currently discussed repeat loop

repeat 5 {
    // code
}

All follow a very basic pattern: The repeat keyword followed by a for declaration of loop scoped variables, then either a condition in the case of while or an iterator in the case of for-in. Optionally the language could include an “always” keyword that would specify a set of statements that were guaranteed to execute after each run of the loop. For example:

repeat for var i = 0 while i < 5 {
    
} always i += 1

If this were the case then all current loop constructs and their features could be subsumed by a single type of loop (including the to-be-removed C style for). This does have the problem of increasing the length of some current loop constructs though, specifically for-in.

Tyler

···

On Dec 8, 2015, at 10:05 PM, André Videla via swift-evolution <swift-evolution@swift.org> wrote:

How about having a default implementation in the standard library for it?
We avoid syntactic sugar and we allow for this very simple yet useful expression.
And we’ve seen solutions in this thread using autoclosurses and extensions

5.times {
   …
}

or a function

repeat(4) {
   …
}

all sound good to me.

- André

On 09 Dec 2015, at 06:58, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Dec 8, 2015, at 4:43 PM, Daniel Steinberg via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

For me this comes up when teaching children or new programmers. (Perhaps not a valid use case)

This is a very valid use case.

FWIW, “repeat N {}” was originally designed and scoped into the Swift 2 implementation of the feature, but was cut due to schedule limitations. There is precedent for this sort of feature in many teaching oriented languages (e.g. Logo).

I’d say that the pro’s and con’s of this are:

+ Makes a simple case very simple, particularly important in teaching.
+ Even if you aren’t familiar with it, you can tell at first glance what the behavior is.
- It is “just syntactic sugar”, which makes the language more complex.
- It is a very narrow feature that is useful in few practical situations.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Sean Heber) #19

This seems like the most sensible thing to me - if you don’t need the variable, just leave it out! Nice and clean and no confusion.

l8r
Sean

···

On Dec 9, 2015, at 3:18 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

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?

You know, I look at this and wonder, why can’t you just say `for 1…5`?

--
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Brent Royal-Gordon) #20

Why not define a times() or timesRepeat() method on Int taking a closure to execute multiple times, like Erica recently posted and which is common in other languages like Ruby, Smalltalk or (I think) Scala:

In the teaching context, the answer is because it doesn’t actually teach you anything that can be extended to other parts of the language. `times` is a great fit for Ruby because many other Ruby looping constructs are also methods on an object (particularly the `each` loop), and because it works with all the usual loop flow control like `break`. None of these things are true about Swift, so you’re effectively teaching your students a dead end.

···

--
Brent Royal-Gordon
Architechies