Collapse filter { p }.<terminal>() chains

Proprietary

Folds predicate-taking terminals (first, last, count, any, none, single, firstOrNull, lastOrNull, singleOrNull) and sumOf/maxOf/minOf selectors into the upstream filter or map, avoiding the intermediate list materialization.

org.openrewrite.kotlin.performance.CollapseFilterTerminals$KtRecipe
mod run . --recipe='org.openrewrite.kotlin.performance.CollapseFilterTerminals$KtRecipe'

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.

Recipe list

It runs these recipes, in this order.

  1. Use first { predicate } instead of filter { predicate }.first()
  2. Use last { predicate } instead of filter { predicate }.last()
  3. Use count { predicate } instead of filter { predicate }.count()
  4. Use any { predicate } instead of filter { predicate }.isNotEmpty()
  5. Use none { predicate } instead of filter { predicate }.isEmpty()
  6. Use firstOrNull { predicate } instead of filter { predicate }.firstOrNull()
  7. Use lastOrNull { predicate } instead of filter { predicate }.lastOrNull()
  8. Use single { predicate } instead of filter { predicate }.single()
  9. Use singleOrNull { predicate } instead of filter { predicate }.singleOrNull()
  10. Use any { predicate } instead of filter { predicate }.any()
  11. Use none { predicate } instead of filter { predicate }.none()
  12. Use sumOf { selector } instead of map { selector }.sum()
  13. Use maxOf { selector } instead of map { selector }.max()
  14. Use minOf { selector } instead of map { selector }.min()
  15. Use mapNotNull { f } instead of map { f }.filterNotNull()
  16. Use firstNotNullOf { f } instead of mapNotNull { f }.first()
  17. Use firstNotNullOfOrNull { f } instead of mapNotNull { f }.firstOrNull()