Basic support for "add" to collection used inside the loop

This commit is contained in:
Valentin Kipyatkov
2016-04-06 19:20:08 +03:00
parent d3721e9462
commit f51c5a19dd
20 changed files with 272 additions and 46 deletions
@@ -0,0 +1,12 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<String> {
val result = ArrayList<String>()
<caret>for (s in list) {
if (s.length > 0) {
result.add(s)
}
}
return result
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<String> {
<caret>val result = list.filter { it.length > 0 }
return result
}
+11
View File
@@ -0,0 +1,11 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val result = ArrayList<Int>()
<caret>for (s in list) {
if (s.length > 0)
result.add(s.hashCode())
}
return result
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val result = list
.filter { it.length > 0 }
.map { it.hashCode() }
return result
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val result = ArrayList<Int>()
<caret>for (s in list) {
if (s.length > result.size) {
result.add(s.hashCode())
}
}
return result
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {
val result = ArrayList<Int>()
<caret>list
.filter { it.length > result.size }
.mapTo(result) { it.hashCode() }
return result
}
+10
View File
@@ -0,0 +1,10 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(map: Map<Int, String>): List<String> {
val result = ArrayList<String>()
<caret>for (s in map.values) {
result.add(s)
}
return result
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.ArrayList
fun foo(map: Map<Int, String>): List<String> {
<caret>val result = map.values.toList()
return result
}
+10
View File
@@ -0,0 +1,10 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<String> {
val result = HashSet<String>()
<caret>for (s in map.values) {
result.add(s)
}
return result
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<String> {
<caret>val result = map.values.toSet()
return result
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<Int> {
val result = HashSet<Int>()
<caret>for (s in map.values) {
result.add(s.length)
}
return result
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
import java.util.HashSet
fun foo(map: Map<Int, String>): Collection<Int> {
<caret>val result = map.values
.map { it.length }
.toSet()
return result
}