Hi guys, I am looking for an elegant way of accomplishing the following:
I have a sorted list of objects ranging from 0 to 43000 (number of items in list)
I would like to get a list of lists where each list contains 5% of the list. [[top 5% of items], [next 5% of items]…]
We should have 20 smaller lists now, I wanted to just have empty lists at the end if we run out of items.
So far I have tried the chunked function
Copy code
val myList = (1..100).toList().chunked(5) -> gives me the right answer
val result = (1..45).toList().chunked(5)) -> gives me only 9 chunks
Is it possible to add empty lists for the remaining 11 chunks?
y
Youssef Shoaib [MOD]
03/02/2021, 9:20 AM
instead it should be based on the length of the list like this:
Copy code
val result = (1..45).toList().let { it.chunked(Math.ceil(it.length / 20f)) }
☝️ 2
p
pdvrieze
03/02/2021, 12:23 PM
You can take the original chunked list/sequence. Add an infinite sequence of empty lists (using one of the sequence generator functions - with or without coroutines) and then use the