I'm trying to refactor the following function to make it more Kotlin-idiomatic using modern functional language features:
fun foobar(): List<Account> {
var pageOffset = 0
val accounts: MutableList<Account> = ArrayList()
var chunk: List<Account> = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
while (chunk.isNotEmpty()) {
accounts.addAll(chunk)
pageOffset = MAX_POLL_SIZE
chunk = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
}
return accounts
}
My first attempt was to replace the mutable list with buildList, but it's still not quite functional style:
fun foobar2(): List<Account> {
var pageOffset = 0
return buildList {
var chunk: List<Account> = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
while (chunk.isNotEmpty()) {
addAll(chunk)
pageOffset = MAX_POLL_SIZE
chunk = accountsService.getAccounts(pageOffset, MAX_POLL_SIZE)
}
}
}
Ideally, I would like to replace the whole while loop with something like accountsService.getAccounts(...).map { ... } but I can't figure out how to refactor a while loop that has this kind of "first chunk" followed by a number of further chunks. Can it be done?
CodePudding user response:
You can do something like this:
fun foobar(): List<Account> =
generateSequence(0) { it MAX_POLL_SIZE }.map { offset ->
accountsService.getAccounts(offset, MAX_POLL_SIZE)
}.takeWhile { it.isNotEmpty() }.flatten().toList()
generateSequence generates an infinite, lazy sequence starting with 0, MAX_POLL_SIZE, MAX_POLL_SIZE * 2, MAX_POLL_SIZE * 3, and so on. This is the sequence of page offsets for which you are getting accounts. We transform each page offset to the list of accounts it corresponds to, using map. After that, we specify an end to the infinite sequence using takeWhile.
Now we have a Sequence<List<Account>>, so we use flatten converts that into a Sequence<Account>, which can then be trivially converted to a List<Account>,
