4 ms·
Three-element for loops are the exception. The modal Go for loop is for idx, elem := range someSlice { } Sometimes with idx replaced by _. (Or at lea
by jerf 11y ago
Three-element for loops are the exception. The modal Go for loop is
for idx, elem := range someSlice {
}
Sometimes with idx replaced by _.
(Or at least, that's the modal for loop if you're doing it right.)
I wish I could make
for x := range someSlice {
default to iterating on values rather than indexes... I do have rather a lot of
for _, val := range someSlice {
but, meh.
Personally I'd unhesitatingly introduce that version and pull out the three-element version once someone needs it. People should learn value-based iteration as the default. I have to admit when I'm doing an interview and I see someone pull out the 3-element for loop in a language that does value-based iteration, I mentally dock a point for choosing a dangerous construct when an easier, safer construct is readily available.
(Before someone jumps up and yells, I readily admit... no, advocate for... Python's
for idx, value in enumerate(something):
when available. I really don't like the 3-element for loop.)
- _yosefk 11y agoWell, yeah, but if you want to iterate over integer ranges, don't you use the for ;; {} form? (If not why is that form even there?) I'd expect the TFA's Mandelbrot set example to need iterating over integer ranges (while most code I'd actually write myself would iterate over elements of sequences.) All I'm saying is, it's a lot of syntax to inhale, there are plenty of kinds of for loop. I certainly don't mind, and I'd be happy enough with "the modal for loop" being DRY and compact. I just recall that my former self had trouble memorizing grammar cases. Python's for loop always does for value in sequence, so there's less syntax.
- pcwalton 11y agoRight, you do use the C-style for loop for numeric iteration. The reason why a more succinct form for numeric iteration isn't there is that Go has no generics, therefore no generic interfaces, therefore no Iterator interface, therefore no iterators. So Go had no choice but to build all syntaxes that look like iterators directly into the language.
- jerf 11y ago"Well, yeah, but if you want to iterate over integer ranges, don't you use the for ;; {} form?" Yes, but in general, needing that is the exception, not the rule. Ironically, a great deal of those exceptions tend to arise when doing toy projects and teaching children, so, I'll grant a solid touche there. :) "Python's for loop always does for value in sequence, so there's less syntax." Well, in a straight-up contest, Go and Python actually sorta... tie, or very nearly. Go's for has four versions (by my count): 1. for x := range y {...}, value iteration 2. for init; cond; post {...}, the traditional 3-element for loop 3. for cond {...}, which is basically the while loop 4. for {...}, "loop forever" the latter being something that is actually used with some frequency by server-like goroutines. In Python, that's: 1. for x in y 2. actually a bit of a pain, but for i, x in range(y) + break usually covers it. (Go has break too, of course.) 3. while cond: 4. while True Net-net, if Python wins, it's not by much. I will give it points for making value iteration the simpler, visibly-obvious preferred alternative. In the general "syntax to inhale", I gotta say Python is the clear loser. It's the clear loser due to being a featureful language that has evolved a lot over the decades, in a way that is generally to its advantage, but in this particular context, no, it's no contest. Python has way more syntax than Go. It is way easier to write idiomatic Python code that someone with 4 weeks of experience in Python will look at and not even know where to start searching the manual to find out what just happened (generator comprehensions in particular can be really sneaky). Go could be covered in its syntactic entirity in a well-paced high school semester. I'm personally a bit ambivalent here. Python has been my goto recommendation for "teach new people with this" language for a good long time, but I am forced to admit that Go is potentially at least competitive, with the right resources created for it. It's a really different spin on the newbie experience, but it has a certain appeal. I'd probably stick with Python until those resources appear, though.
- pcwalton 11y agoI think (2) in Python is a clear winner over Go's C-style for loops, which are pretty bad all around: they're hard to understand for beginners, they're verbose, they make it easy to get boundary conditions wrong (especially with unsigned types!), etc. etc.
- laumars 11y ago> Well, yeah, but if you want to iterate over integer ranges, don't you use the for ;; {} form? (If not why is that form even there?) for range does iterate over integer arrays / slices. The reason the C-style for loop exists is because sometimes you don't want to iterate the entire stack. eg // iterate half the stack: for i := 0; i < len(slice) / 2; i++ {} // iterate with a stepping: for i := 0; i < len(slice); i+=2 {} // iterate from a different starting position: for i := 5; i < len(slice); i++ {} // reverse iteration: for i := len(slice)-1; i>=0; i-- {}
- infogulch 11y agoSlicing is free and can be done inline, so your first and third examples can be changed to the range syntax: // iterate half the stack: for i := range slice[:len(slice)/2] {} // iterate from a different starting position: for i := range slice[5:] {} That still leaves stepping and reverse, but I've found those are relatively rare, especially compared to the other two.
- laumars 11y agoOoops, just seen this comment. Yes, you're right, your slice techniques do make a lot more sense than my examples.