Is it possible to have a for-loop in Kotlin with a dynamic step (so it can change)?
I’m having trouble translating the following Java-code into Kotlin:
for(int i = 0; i < 500; i+= (i < 40 ? 10 : 20)) {
System.out.println("Now at "+ i);
}
Is this possible?
I’ve read in an older post (How to for loop with var i) that there is no way to mutate i, so am I right to assume I’ll have to use while?
I think, that you should have never used for loop for that in the first place and it is good that kotlin does not allow this usage. If I understand correctly, in your example variable i is not some kind of index, it has some meaning. And using such variables inside for loop could lead to some nasty side effects in a large code.
Variant using while is not much longer:
var i = 0
while( i < 500 ){
println("Now at $i")
i += if( i < 40 ) 10 else 20
}
Pitfall number one: You accidentally or intentionally changing i somewhere inside your loop. You should obviously never do it, but changing the increment is the first step in this direction.
Pitfall number two: For some reason, you want to change the behavior of your dynamic step. You do it in one place, but forget to do in another and now you have two iterations with different number of steps.
Pitfall number three: Suppose you want a more complicated behavior and need three different steps instead of two. You won’t be able to do it with for anymore. You will have to either delegate step calculation to additional function which is even worse, or rewrite everything. In my example you can do it easily by replacing if by when.
Pitfall number four occurs when you have some parallel operations messing with i. In my example, you can simply replace i by AtomicInteger and make your operations thread safe.
There could be additional motivation, but in general I think that classic for loop should be deprecated and replaced by functional-style operations wherever it is possible.
You are right,
however an Iterable plus another one produces an Iterable,
a Range plus another one produces a List.
Your code allocates the entire array an some -generally- hundreds of Integer (but it is more readable).
When I read threads like this I feel like I fell into a Bizzaro Universe where the simplicity of a C for loop with its explicitly exposed initialization, looping condition and next iteration stepping, without any hidden costs that you have to “keep in mind” beyond what you write, is considered undesirable.
Only to be followed by “new improved” language constructs that go into a long discussion of Iterable vs Sequence vs range operations, to wind up with a lot more code and hidden implications that must be considered (instantiation of the full list being one, foreach being about 3x slower than explicit for loop is another).
The while loop does create equivalent code but the beauty of C-for syntax was to eliminate such ugly boiler plate in the first place.
Please, don’t tell me that you are confused by C-for loop’s complex syntax or difficulty of intuiting its operation. The examples of Kotlin code you provide speak volumes to your ability to handle complexity and grasp non-intuitive constructs.
Why not admit that elimination of some language features in Kotlin, even when necessitated by other concerns, is a regression not evolution, which we live with to enjoy real improvements it offers.
Justifying it with code that resembles “perl poetry” or “obfuscated C” compared to the elegance of a C for loop is trying to put lipstick on a pig.
Please, don’t tell me you’ve ever accidentally blown past the end of a C array and randomly written crap into memory.
All programming is about tradeoffs. On modern hardware, my preference is to favor constructs that protect developers from making dumb catastrophic mistakes.