Hi guys, I am looking for an elegant way of accomp...
# announcements
v
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
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
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
takeN
function to pick the amount you actually want.