【发布时间】:2018-12-01 01:05:51
【问题描述】:
获取列表并将其分组为大小为 n 的元组的惯用方式是什么?
例如:用三元组分成三组
val list = listOf(1,2,3,4)
val partitioned = list.groupsOf(3)
// partitioned[0] = List<Int> 1, 2, 3
// partitioned[1] = List<Int> 4
但最好是这样的
val list = listOf(1,2,3,4)
val newList = mutableListOf()
list.forGroupsOf(3) { triple: Triple<Int?> ->
newList.add( triple )
}
// partitioned[0] = Triple<Int?> 1, 2, 3
// partitioned[1] = Triple<Int?> 4, null, null
注意:List.groupsOf 和 List.forGroupsOf 这个例子是我编造的
【问题讨论】:
标签: list kotlin functional-programming partition