take所在位置是kotlin.sequences.take,其相关用法介绍如下。

用法:

fun <T> Sequence<T>.take(n: Int): Sequence<T>

返回包含第一个 n 元素的序列。

操作是 intermediatestateless

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z]
//sampleEnd
}

输出:

[a, b, c]
[a, b, c, d, e]
[y, z]
[x, y, z]

异常

IllegalArgumentException- 如果n是负数。