https://kotlinlang.org logo
g

gaetan

03/05/2017, 9:29 AM
Copy code
fun <T> List<T>.batch(batchSize:Int): List<List<T>> {
    var startIndex = 0
    val ret = mutableListOf<List<T>>()
    while (startIndex < this.size){
        ret.add(subList(startIndex, (startIndex + batchSize ).coerceAtMost(size)))
        startIndex += batchSize
    }
    return ret
}
👍 3