0
Follow
0
View

Kotlin Arrays: Lookahead without going out of bounds?

dnrddnr 注册会员
2023-01-25 14:50

windowed(2) or zipWithNext() lets you slide along pairs of adjacent values without going past the bounds, but it's only available for Iterables and Sequences, not arrays:

fun dontCrash(a: Array): List {
    a.asSequence()
        .zipWithNext()
        .mapIndexedNotNull { i, (a, b) -> i.takeIf { a && !b } }
        .toList()
}

About the Author

Question Info

Publish Time
2023-01-25 14:50
Update Time
2023-01-25 14:50

Related Question

Kotlin(如果有的话)

将List of array附加到一个数组,并在kotlin中重复此操作

委托给Kotlin中的属性的属性

是否可以使用python和kotlin来构建android项目?

共享偏好,复选框,Kotlin, Android

模块是用react原生中不兼容的Kotlin版本编译的

属性必须初始化,即使类型在Kotlin中定义

Kotlin Smart Cast on when expression

检测从相机Android-Kotlin捕获的图像的模糊性

使用Kotlin在Fragment中Retrofit2参数