I need to convert some Java code to Scala. I have such source. How to rewrite it in Scala? Question may be simple. But it match harder then for(i <- 1 until 10) {} examples in documentation.
for (int i = password.length(); i != 0; i >>>=1)
{ some code }
King regards, Alexey
If you want it to be as fast as possible--which I am assuming is the case given the shift operation--then you should use a while loop:
var i = password.length()
while (i != 0) {
// some code
i >>>= 1
}
It's one of the few cases where Java is more compact than Scala given the same operation.
You could also use tail recursion:
final def passwordStuff(password: Password)(i: Int = password.length): Whatever = {
if (i != 0) {
// Some code
passwordStuff(password)(i >>> 1)
}
}
which will compile into something of the same speed as the while loop (in almost every case, anyway).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With