Ran Magen
09/16/2019, 4:56 PM(T) -> (R?)
and then mapNotNull
? That'd work (assuming T : Any
) but it would create an intermediate list, which ideally I'd like to avoid.Evan R.
09/16/2019, 5:11 PM.asSequence()
. Then you can just do:
val sampleList = listOf(1, 2, 3)
val filterMappedList = sampleList.asSequence().filter { it > 1 }.map { it * 2 }.toList()
arekolek
09/16/2019, 8:41 PMyou mean by using aNo, I think Karel meant:and then(T) -> (R?)
?mapNotNull
inline fun <T, R : Any> List<T>.collect(
predicate: (T) -> Boolean,
transform: (T) -> R
): List<R> {
return mapNotNull {
if (predicate(it)) {
transform(it)
} else {
null
}
}
}
karelpeeters
09/16/2019, 10:35 PM