Java-like for statements are not supported, right?...
# announcements
l
Java-like for statements are not supported, right? I was following Uncle Bob's prime factor kata, and something similar to this was made in java:
Copy code
for(int divisor = 2; n > 1; divisor++) {
    for(; n % divisor == 0; n /= divisor) {
        ...
    }
}
s
There’s almost definitely a more idiomatic way to write this code piece that isn’t nigh unreadable
r
Honestly I always hated for loops written like this. It feels unnecessarily obfuscated and hard to follow in my opinion. Basically just a way to save 2 lines instead of writing a
while
loop. I prefer the
while
version.
👍🏿 1
💯 1
s
You could even write it using tail recursion, like some snippets I found on DDG.
a
In simple terms, a for loop does this...
Copy code
for (initialization; termination; increment) {
    statement(s)
}
Which is pretty much the same as this except for the fact that the initialization is scoped within the loop.
Copy code
initialization
while(termination) {
    statement(s)
    increment
}
s
And the latter is more readable anyway
a
Back in the day, we called em control breaks. Which were used often when generating reports. COBOL (cough cough). 😎
l
Hmm.. IDK, these two fors have a single line in them, and I feel that the method is very clear on what it's doing
Perhaps this was an exagerated example, but I don't think it's possible, right?
a
It seems to be missing something. N is never instantiated or initialized in that code.
It sort of looks like a do until wrapped by a do while.
Probably not right but off the top of my head it might look something like this:
Copy code
var n... comes from somewhere???

var divisor:int = 2;
while(n > 1) {
    do {
        ...

        n /= divisor
    } while (n % divisor == 0)

    divisor++
}
s
I like to think I’m pretty knowledgeable of Kotlin/Java and imperative programming in general. If you hadn’t mentioned what that snippet was actually for, I wouldn’t have been able to tell you.
l
I would usually give you the method name. You wouldn't be able to tell me if it was in the while format either