Collection
API包含onEach
函数(https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/on-each.html),其功能类似于forEach
的非终端同级。
为什么没有为onEach
定义Array
?
答案 0 :(得分:1)
“为什么”可能不是这里要回答的东西,可能是由于许多原因,包括疏忽,尚未完成的某些事情,一个非常好的理由(一个坏主意)或其他原因。您可以在https://youtrack.jetbrains.com/issues/KT中提交错误/请求(或查看是否存在错误/请求)。
与此同时,您可以轻松拥有自己的:
inline fun <T> Array<T>.onEach(action: (T) -> Unit): Array<T> {
return apply { for (element in this) action(element) }
}
inline fun IntArray.onEach(action: (Int) -> Unit): IntArray {
return apply { for (element in this) action(element) }
}
// and a version for each primitive array type would need to be created...