David Kubecka
03/09/2023, 1:26 PMsomeCollection.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> .ephemient
03/09/2023, 1:27 PMsomeCollection.mapNotNull {
val result = someExpensiveComputation(it) ?: return@mapNotNull null
it to result
}ephemient
03/09/2023, 1:28 PMsomeCollection.mapNotNull { someExpensiveComputation(it)?.let(it::to) }
but I don't think it's clearerDavid Kubecka
03/09/2023, 1:31 PMephemient
03/09/2023, 1:32 PMif (result != null) it to result else null
or the equivalent ?.let if you wanted, I just prefer the early return to the additional nested scopeDavid Kubecka
03/09/2023, 1:36 PMto (have to invent an alias for the inner it, though). Btw it didn't occur to me that it can be used this way in its prefix variant but of course it makes sense.