Collapse filter { p }.<terminal>() chains
ProprietaryFolds 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$KtRecipemod 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.
- Use
first { predicate }instead offilter { predicate }.first() - Use
last { predicate }instead offilter { predicate }.last() - Use
count { predicate }instead offilter { predicate }.count() - Use
any { predicate }instead offilter { predicate }.isNotEmpty() - Use
none { predicate }instead offilter { predicate }.isEmpty() - Use
firstOrNull { predicate }instead offilter { predicate }.firstOrNull() - Use
lastOrNull { predicate }instead offilter { predicate }.lastOrNull() - Use
single { predicate }instead offilter { predicate }.single() - Use
singleOrNull { predicate }instead offilter { predicate }.singleOrNull() - Use
any { predicate }instead offilter { predicate }.any() - Use
none { predicate }instead offilter { predicate }.none() - Use
sumOf { selector }instead ofmap { selector }.sum() - Use
maxOf { selector }instead ofmap { selector }.max() - Use
minOf { selector }instead ofmap { selector }.min() - Use
mapNotNull { f }instead ofmap { f }.filterNotNull() - Use
firstNotNullOf { f }instead ofmapNotNull { f }.first() - Use
firstNotNullOfOrNull { f }instead ofmapNotNull { f }.firstOrNull()