Collapse sorted().first/last() and reversed().first/last() chains
ProprietaryReplaces O(n log n) sorts whose only purpose is to read one element with the equivalent O(n) min/max/minBy/maxBy, and elides unnecessary reversed() copies before first/last.
org. openrewrite. kotlin. performance. CollapseSortAndReverse$KtRecipemod run . --recipe= 'org. openrewrite. kotlin. performance. CollapseSortAndReverse$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
min()instead ofsorted().first() - Use
max()instead ofsorted().last() - Use
max()instead ofsortedDescending().first() - Use
min()instead ofsortedDescending().last() - Use
minBy { selector }instead ofsortedBy { selector }.first() - Use
maxBy { selector }instead ofsortedBy { selector }.last() - Use
last()instead ofreversed().first() - Use
first()instead ofreversed().last() - Use
sortedByDescending { f }instead ofsortedBy { f }.reversed() - Use
sortedDescending()instead ofsorted().reversed() - Use
mapTo(mutableListOf(), f)instead ofmap(f).toMutableList() - Use
filterTo(mutableListOf(), p)instead offilter(p).toMutableList() - Use
filterNotTo(mutableListOf(), p)instead offilterNot(p).toMutableList() - Use
flatMapTo(mutableListOf(), f)instead offlatMap(f).toMutableList()