Merging subsequent .filter()'s

This commit is contained in:
Valentin Kipyatkov
2016-04-05 22:39:08 +03:00
parent 6dbd9c944a
commit fcbf68617e
11 changed files with 75 additions and 17 deletions
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue
if (s.length < 10 && s != "abc") {
if (s == "def") continue
val s1 = s + "x"
return s1
}
}
return null
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun foo(list: List<String>): String? {
return list
.filter { !it.isEmpty() && it.length < 10 && it != "abc" && it != "def" }
.map { it + "x" }
.firstOrNull()
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue
if (s.length < 10 && s != "abc") {
if (s == "def") continue
return s
}
}
return null
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { !it.isEmpty() && it.length < 10 && it != "abc" && it != "def" }
}