Alex Kuznetsov
07/28/2024, 8:26 PMfun<T> List<T>.indexedNotNull(): List<Pair<Int, T>> = this.mapIndexedNotNull { index, t ->
t?.let{ index to t }
}
Joffrey
07/28/2024, 8:32 PMT & Any
, it's the only denotable intersection type for now, but that's the one you need so you're lucky :)ephemient
07/28/2024, 8:46 PMfun <T: Any> List<T?>.indexedNotNull(): List<Pair<Int, T>>
represents that fine (in the same way that .filterNotNull()
does)ephemient
07/30/2024, 7:02 AM.withIndex()
is lazy, which you could also replicate pretty easily
fun <T: Any> Iterable<T?>.indexedNotNull(): Iterable<IndexedValue<T>> = Iterable {
iterator {
forEachIndexed { index, value ->
if (value != null) yield(IndexedValue(index, value))
}
}
}
Alex Kuznetsov
07/30/2024, 1:21 PMforEachIndexed