unzip所在位置是kotlin.sequences.unzip,其相關用法介紹如下。

用法:

fun <T, R> Sequence<Pair<T, R>>.unzip(): Pair<List<T>, List<R>>

返回一對列表,其中 first 列表是根據該序列中每對的第一個值構建的,second 列表是根據該序列中每對的第二個值構建的。

操作是 terminal

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val result = generateSequence(0 to 1) { it.first + 1 to it.second * 2 }.take(8).unzip()

println(result.first.toList()) // [0, 1, 2, 3, 4, 5, 6, 7]
println(result.second.toList()) // [1, 2, 4, 8, 16, 32, 64, 128]
//sampleEnd
}

輸出:

[0, 1, 2, 3, 4, 5, 6, 7]
[1, 2, 4, 8, 16, 32, 64, 128]