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

用法一

fun CharSequence.any(): Boolean

如果 char 序列至少有一个字符,则返回 true

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.any() is ${emptyList.any()}") // false

val nonEmptyList = listOf(1, 2, 3)
println("nonEmptyList.any() is ${nonEmptyList.any()}") // true
//sampleEnd
}

输出:

emptyList.any() is false
nonEmptyList.any() is true

用法二

inline fun CharSequence.any(
    predicate: (Char) -> Boolean
): Boolean

如果至少有一个字符与给定的 predicate 匹配,则返回 true

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.any { isEven(it) } is ${zeroToTen.any { isEven(it) }}") // true
println("zeroToTen.any(isEven) is ${zeroToTen.any(isEven)}") // true

val odds = zeroToTen.map { it * 2 + 1 }
println("odds.any { isEven(it) } is ${odds.any { isEven(it) }}") // false

val emptyList = emptyList<Int>()
println("emptyList.any { true } is ${emptyList.any { true }}") // false
//sampleEnd
}

输出:

zeroToTen.any { isEven(it) } is true
zeroToTen.any(isEven) is true
odds.any { isEven(it) } is false
emptyList.any { true } is false