Premraj Motling
10/24/2020, 6:29 AMfun loadPage(offset: Int, limit: Int): List<Int> {
println("Called for offset $offset and limit $limit")
if (offset == 300) {
return listOf(offset + 10) // return 310 - last element
}
return (offset..offset + limit).take(limit)
}
fun paginatedCall() {
val limit = 100
var hasMore = true
val total = generateSequence(0) { if (hasMore) it + limit else null }
.map { loadPage(it, limit) }
.fold(0) { c, list ->
hasMore = list.size >= limit
println("Consumed list of size ${list.size}")
c + list.size
}
println("Total - $total")
}
paginatedCall()
Was wondering if there's any better way to implement paginatedCall
(assume loadPage
fetches records from DB)