How to best implement a pattern where I want to compute a value for each element in a collection and return those element+value pairs where the value is not null)? Currently, I have simply this
Copy code
someCollection.map { it to someExpensiveComputation(it) }.filter { it.second != null }
which works but the return type is
List<T, R?>
and I of course want
List<T, R>
.
e
ephemient
03/09/2023, 1:27 PM
Copy code
someCollection.mapNotNull {
val result = someExpensiveComputation(it) ?: return@mapNotNull null
it to result
}