andrewoma
03/07/2017, 3:59 AMfun <T> split(values: List<T>, vararg lengths: Int): Sequence<List<T>> {
val lengthsSeq = lengths.iterator()
val valuesSeq = values.iterator()
return generateSequence { if (lengthsSeq.hasNext()) List<T>(lengthsSeq.next()) { valuesSeq.next() } else null }
}
val result = split(listOf("one", "two", "three", "four", "five", "six", "seven", "eight"), 3, 2, 3).associateBy { it.first() }
assertEquals(result, mapOf(
"one" to listOf("one", "two", "three"), //row length == 3
"four" to listOf("four", "five"), //len == 2
"six" to listOf("six", "seven", "eight") //len == 3
))
groostav
03/07/2017, 11:06 AMassociateBy
, I was just fairly certain that groupBy
was going to be the mechanism to do it. a List<List<T>>
is sufficient