Reputation: 11
I'm very new in kotlin and wanted to solve following problem with a do while: I want to create a hash and want to check if there is the same hash stored in a key-value store as a key. In java I would make it with a String variable which I declared outside the while. But that will only work with a var in Kotlin and I learned that it is common practise to avoid var. My code looks as following (with var...)
var hash = ""
do {
hash = createHash(longUrl)
val optional = shortUrlRepository.findById(hash)
} while(optional.isPresent)
What would you say is the best way to solve this?
thank you a lot!
Upvotes: 1
Views: 323
Reputation: 111
Maybe something like this?
val hash = generateSequence { createHash(longUrl) }
.first { !shortUrlRepository.findById(it).isPresent }
... and of course, you can always localize var
and pass it outside as val
.
val someVal = run {
var someVar: String = ""
// do super logic with var
someVar
}
...
Upvotes: 1