Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!