Do not generate redundant .toList()

This commit is contained in:
Valentin Kipyatkov
2016-04-22 12:56:25 +03:00
parent 813060fef3
commit aa6240a0c5
8 changed files with 89 additions and 4 deletions
@@ -378,7 +378,7 @@ class AssignToListTransformation(
get() = "toList()"
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
if (previousTransformation !is FilterTransformation) return null
//TODO: can be any SequenceTransformation's that return not List<T>? Also this code needs to be changed when .asSequence() used
return AssignSequenceTransformationResultTransformation(previousTransformation, initialization)
}
@@ -76,6 +76,22 @@ class FilterTransformation(
*/
object Matcher : SequenceTransformationMatcher {
override fun match(state: MatchingState): SequenceTransformationMatch? {
var (transformation, currentState) = matchOneTransformation(state, takeWhileAllowed = true) ?: return null
/*
if (transformation is FilterTransformation || transformation is FilterIndexedTransformation) {
while (true) {
val (nextTransformation, nextState) = matchOneTransformation(currentState, takeWhileAllowed = false) ?: break
if (nextTransformation !is FilterTransformation && nextTransformation !is FilterIndexedTransformation) break
}
}
*/
return SequenceTransformationMatch(transformation, currentState)
}
private fun matchOneTransformation(state: MatchingState, takeWhileAllowed: Boolean): Pair<SequenceTransformation, MatchingState>? {
val ifStatement = state.statements.firstOrNull() as? KtIfExpression ?: return null
if (ifStatement.`else` != null) return null
val condition = ifStatement.condition ?: return null
@@ -84,7 +100,7 @@ class FilterTransformation(
if (state.statements.size == 1) {
val transformation = createFilterTransformation(state.outerLoop, state.inputVariable, state.indexVariable, condition, isInverse = false)
val newState = state.copy(statements = listOf(then))
return SequenceTransformationMatch(transformation, newState)
return transformation to newState
}
else {
val statement = then.blockExpressionsOrSingle().singleOrNull() ?: return null
@@ -93,14 +109,15 @@ class FilterTransformation(
if (statement.targetLoop() != state.innerLoop) return null
val transformation = createFilterTransformation(state.outerLoop, state.inputVariable, state.indexVariable, condition, isInverse = true)
val newState = state.copy(statements = state.statements.drop(1))
return SequenceTransformationMatch(transformation, newState)
return transformation to newState
}
is KtBreakExpression -> {
if (!takeWhileAllowed) return null
if (statement.targetLoop() != state.outerLoop) return null
val transformation = TakeWhileTransformation(state.outerLoop, state.inputVariable, condition.negate())
val newState = state.copy(statements = state.statements.drop(1))
return SequenceTransformationMatch(transformation, newState)
return transformation to newState
}
else -> return null
@@ -108,6 +125,8 @@ class FilterTransformation(
}
}
// private fun mergeFilterTransformations(transformation1: )
//TODO: choose filter or filterNot depending on condition
private fun createFilterTransformation(
loop: KtForExpression,
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexed{}'"
import java.util.*
fun foo(list: List<String>): List<String> {
val result = ArrayList<String>()
<caret>for ((index, s) in list.withIndex()) {
if (s.length > index) {
result.add(s)
}
}
return result
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexed{}'"
import java.util.*
fun foo(list: List<String>): List<String> {
val <caret>result = list.filterIndexed { index, s -> s.length > index }
return result
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull()'"
import java.util.ArrayList
fun foo(list: List<String?>): List<String> {
val result = ArrayList<String>()
<caret>for (s in list) {
if (s != null) {
result.add(s)
}
}
return result
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull()'"
import java.util.ArrayList
fun foo(list: List<String?>): List<String> {
val <caret>result = list.filterNotNull()
return result
}
+14
View File
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val result = ArrayList<Int>()
<caret>for (s in list) {
if (s.length > 0) {
val h = s.hashCode()
result.add(h)
}
}
return result
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val <caret>result = list
.filter { it.length > 0 }
.map { it.hashCode() }
return result
}