How to chunk a list in specific value?
Ex: split where current value is 5
val x = listOf(1,2,3,4,5,2,3,1,5,4,1,5)
convert x to this:
x => [[1,2,3,4,5],[2,3,1,5],[4,1,5]]
How to chunk a list in specific value?
Ex: split where current value is 5
val x = listOf(1,2,3,4,5,2,3,1,5,4,1,5)
convert x to this:
x => [[1,2,3,4,5],[2,3,1,5],[4,1,5]]
Copyright © 2021 Jogjafile Inc.
Surprisingly, there isn't a method to do this in the standard library. But it's not hard to write one yourself, e.g.:
That does as required:
It also handles all the corner cases: an empty list, consecutive separators, and zero or more leading and/or trailing separators, e.g.:
(It would be a good idea to include unit tests covering all such cases, of course.)