Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's unclean about for loops?


For loops require explicit and error-prone use of integer indices and are of limited utility outside arrays. Foreach loops in Java/C# are an attempt to generalise, but they are really just syntactic sugar over external iterators. They give privileged status to the specific iteration interface chosen, entangling the language and its libraries. The actual Iterator interface chosen by Java has proven to be unsuitable for coroutines/yield, but is now too difficult to change. In Haskell, foreach loops can be implemented purely as a higher-order library function.


Yeah, I was meaning 'for loops' to include the modern sense of foreach loops too. Java doesn't distinguish and Kotlin doesn't have the old style at all.

There's no rule that says a for loop must only support a single iteration interface. That seems more like an implementation detail of the compiler than a problem with for loops in general.


Of course the big elephant in the room that I didn't mention, is that for loops get work done via mutation. This is perhaps what was mean't by “unclean".


Mutating index state variables, and it's not functional (you can't pass a function to be the inner loop)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: