else clause for loops like that in Python


(Amir Michail) #1

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
  if i == 5 { break }
} nobreak {
  // no break occurred
}


(Brent Royal-Gordon) #2

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
if i == 5 { break }
} nobreak {
// no break occurred
}

I’ve often wanted an else clause on loops—but one that would run only if the loop went through zero iterations. I can’t imagine when I would ever use `nobreak`.

···

--
Brent Royal-Gordon
Architechies


(Alex Chan) #3

I’d be inclined not to add this feature.

My impression is that (in Python) many people have either never come across this feature or find its behaviour confusing. I don’t think it’s a particularly widely used feature, and I don’t think it’s necessary for Swift.

If we do add it, I strongly agree with changing the name. One of the biggest confusions in Python is the mismatch between if…else and for…else. Renaming it would help.

— Alex

(For the non-Python programmers on the list: the “else” is an optional branch at the end of for/while loops that runs only if the loop exits cleanly/doesn’t break.)

···

On 8 Dec 2015, at 18:35, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
if i == 5 { break }
} nobreak {
// no break occurred
}

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


(Jordan Rose) #4

Yep, this is the one I'd want. But I think we probably just shouldn't have either, especially now that we've seen that there are two possible interpretations. Amir, a workaround for yours today:

outer: do {
  for i in 0..<10 {
    if i == 5 { break outer }
  }
  print("no break occurred")
}

Jordan

···

On Dec 8, 2015, at 14:50, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
if i == 5 { break }
} nobreak {
// no break occurred
}

I’ve often wanted an else clause on loops—but one that would run only if the loop went through zero iterations. I can’t imagine when I would ever use `nobreak`.


(Amir Michail) #5

I’d be inclined not to add this feature.

My impression is that (in Python) many people have either never come across this feature or find its behaviour confusing. I don’t think it’s a particularly widely used feature, and I don’t think it’s necessary for Swift.

If we do add it, I strongly agree with changing the name. One of the biggest confusions in Python is the mismatch between if…else and for…else. Renaming it would help.

Maybe it’s not used much in python because the name is confusing? “nobreak” would be better than “else”.

···

On Dec 8, 2015, at 4:50 PM, Alex Chan <alex@alexwlchan.net> wrote:

— Alex

(For the non-Python programmers on the list: the “else” is an optional branch at the end of for/while loops that runs only if the loop exits cleanly/doesn’t break.)

On 8 Dec 2015, at 18:35, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
if i == 5 { break }
} nobreak {
// no break occurred
}

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


(Amir Michail) #6

But to avoid confusion, maybe rename “else” to “nobreak”:

for i in 0..<10 {
if i == 5 { break }
} nobreak {
// no break occurred
}

I’ve often wanted an else clause on loops—but one that would run only if the loop went through zero iterations. I can’t imagine when I would ever use `nobreak`.

What about searching for something in an array? The not found code would go in the nobreak clause.

···

On Dec 8, 2015, at 5:50 PM, Brent Royal-Gordon <brent@architechies.com> wrote:

--
Brent Royal-Gordon
Architechies


(Brent Royal-Gordon) #7

What about searching for something in an array? The not found code would go in the nobreak clause.

For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.

If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.

I’m not saying it’s a useless feature, just that *I* can’t imagine using it.

···

--
Brent Royal-Gordon
Architechies


(Donnacha Oisín Kidney) #8

Be careful with lazy.filter.first! It evaluates the whole sequence, usually.

http://swiftstub.com/122568159/

···

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

What about searching for something in an array? The not found code would go in the nobreak clause.

For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.

If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.

I’m not saying it’s a useless feature, just that *I* can’t imagine using it.

--
Brent Royal-Gordon
Architechies

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


(Donnacha Oisín Kidney) #9

Although I see now that it doesn’t for arrays. ¯\_(ツ)_/¯

···

On 9 Dec 2015, at 01:21, Donnacha Oisín Kidney <oisin.kidney@gmail.com> wrote:

Be careful with lazy.filter.first! It evaluates the whole sequence, usually.

http://swiftstub.com/122568159/

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

What about searching for something in an array? The not found code would go in the nobreak clause.

For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.

If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.

I’m not saying it’s a useless feature, just that *I* can’t imagine using it.

--
Brent Royal-Gordon
Architechies

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


(Kametrixom Tikara) #10

Someone asked such a question on SO recently, I found out if one doesn't want to evaluate the whole sequence, functional style doesn't work: http://stackoverflow.com/a/34032744/3443689

···

On 09 Dec 2015, at 02:21, Donnacha Oisín Kidney via swift-evolution <swift-evolution@swift.org> wrote:

Be careful with lazy.filter.first! It evaluates the whole sequence, usually.

http://swiftstub.com/122568159/

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

What about searching for something in an array? The not found code would go in the nobreak clause.

For an array, I would almost always use `if let` with `lazy.filter.first` or `indexOf`. The if branch is “found”, else is “not found”.

If that wasn’t suitable for some reason, I’d probably put the search loop like that in its own function or method and return early from inside the loop. If you reach the code after the loop, the search has failed.

I’m not saying it’s a useless feature, just that *I* can’t imagine using it.

--
Brent Royal-Gordon
Architechies

_______________________________________________
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