avoiding multiple list iterations via mapNotNull

This commit is contained in:
Adam Brown 2021-10-21 12:03:41 +01:00
parent be67836a3e
commit b146501f29
1 changed files with 3 additions and 1 deletions

View File

@ -27,4 +27,6 @@ data class ProcessedEvent<T>(
}
}
fun <T> List<ProcessedEvent<T>>.onlyKeptEvents() = filter { it.type == ProcessedEvent.Type.KEEP }.map { it.event }
fun <T> List<ProcessedEvent<T>>.onlyKeptEvents() = mapNotNull { processedEvent ->
processedEvent.event.takeIf { processedEvent.type == ProcessedEvent.Type.KEEP }
}