package pl.mg6.likeornot.commons

fun <T> List<T>.batch(chunkSize: Int): List<List<T>> {
    if (chunkSize <= 0) {
        throw IllegalArgumentException("chunkSize must be greater than 0")
    }
    return mapIndexed { index, item -> IndexedItem(index, item) }
            .groupBy { it.index / chunkSize }
            .map { it.value.map { it.item } }
}

private class IndexedItem<T>(val index: Int, val item: T)

package pl.mg6.likeornot.commons

fun <T> List<T>.batch(chunkSize: Int): List<List<T>> {
    if (chunkSize <= 0) {
        throw IllegalArgumentException("chunkSize must be greater than 0")
    }
    val list = mutableListOf<MutableList<T>>()
    for (i in 0 until size) {
        if (i % chunkSize == 0) {
            list.add(mutableListOf())
        }
        list.last().add(get(i))
    }
    return list
}

Make batching code more performant.
Make batching code more performant.

mg6maciej/fluffy-octo-rotary-phone,mg6maciej/fluffy-octo-rotary-phone,mg6maciej/fluffy-octo-rotary-phone