Recognizing manually incremented index

This commit is contained in:
Valentin Kipyatkov
2016-04-21 13:40:03 +03:00
parent 14e87b1f2c
commit d61daed461
23 changed files with 283 additions and 37 deletions
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexed{}.firstOrNull()'"
fun foo(list: List<String>): String? {
var i = 0
<caret>for (s in list) {
if (s.length > i) {
return s
}
i++
}
return null
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexed{}.firstOrNull()'"
fun foo(list: List<String>): String? {
<caret>return list
.filterIndexed { i, s -> s.length > i }
.firstOrNull()
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...flatMap{}.filterNot{}.mapTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
var i = 0
<caret>for (s in list) {
if (i % 10 != 0) {
for (j in s.indices) {
if (j == 10) continue
target.add(j.toString())
}
}
i++
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...flatMap{}.filterNot{}.mapTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
<caret>list
.filterIndexed { i, s -> i % 10 != 0 }
.flatMap { it.indices }
.filterNot { it == 10 }
.mapTo(target) { it.toString() }
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>, target: MutableCollection<String>): String? {
var i = 0
<caret>for (s in list) {
if (s.length > i++) {
target.add(s)
}
i++
}
return null
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>): String? {
var i = 1
<caret>for (s in list) {
if (s.length > i) {
return s
}
i++
}
return null
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>, target: MutableCollection<String>): String? {
var i = 0
<caret>for (s in list) {
if (s.length > i) {
target.add(s)
}
i++
}
println(i)
return null
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>, target: MutableCollection<String>): String? {
var i = 0
<caret>for (s in list) {
if (s.hashCode() % i == 0) continue
if (s.length > i) {
target.add(s)
}
i++
}
println(i)
return null
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= filterIndexed{}.filterIndexed{}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
var j = 0
<caret>for ((i, s) in list.withIndex()) {
if (s.length > i) continue
if (s.length % j == 0) {
target.add(s)
}
j++
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= filterIndexed{}.filterIndexed{}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
<caret>target += list
.filterIndexed { i, s -> s.length <= i }
.filterIndexed { j, s -> s.length % j == 0 }
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>, target: MutableCollection<Int>) {
var j = 0
<caret>for ((i, s) in list.withIndex()) {
val x = s.length + i
if (x < i * j) {
target.add(x)
}
j++
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= ...filterNot{}.mapIndexed{}'"
fun foo(list: List<String>, target: MutableCollection<Int>) {
var i = 0
<caret>for (s in list) {
for (j in s.indices) {
if (j == 10) continue
target.add(i + j)
i++
}
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= ...filterNot{}.mapIndexed{}'"
fun foo(list: List<String>, target: MutableCollection<Int>) {
<caret>target += list
.flatMap { it.indices }
.filterNot { it == 10 }
.mapIndexed { i, j -> i + j }
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNot{}.mapIndexed{}.firstOrNull{}'"
fun foo(list: List<String>): Int? {
var index = 0
<caret>for (s in list) {
if (s.isBlank()) continue
val x = s.length * index
if (x > 0) return x
index++
}
return null
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNot{}.mapIndexed{}.firstOrNull{}'"
fun foo(list: List<String>): Int? {
return list
.filterNot { it.isBlank() }
.mapIndexed { index, s -> s.length * index }
.firstOrNull { it > 0 }
}