Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Functional equivalent of this loop

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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>,

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda