vod*_*dan 87 foreach lambda loops kotlin
科特林具有非常不错的迭代功能,如forEach或repeat,但我不能让break和continue运营商与他们(包括本地和非本地)的工作:
repeat(5) {
break
}
(1..5).forEach {
continue@forEach
}
Run Code Online (Sandbox Code Playgroud)
目标是模拟通常的循环,其功能语法尽可能接近.在某些旧版本的Kotlin中肯定是可能的,但我很难重现语法.
问题可能是标签的错误(M12),但我认为第一个例子应该可行.
在我看来,我已经阅读过关于特殊技巧/注释的某处,但我找不到关于这个主题的任何参考.可能如下所示:
public inline fun repeat(times: Int, @loop body: (Int) -> Unit) {
for (index in 0..times - 1) {
body(index)
}
}
Run Code Online (Sandbox Code Playgroud)
s-h*_*ter 83
这将打印1到5.这些return@forEach行为类似于continueJava中的关键字,这意味着在这种情况下,它仍然执行每个循环但如果值大于5则跳到下一次迭代.
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
nums.forEach {
if (it > 5) return@forEach
println(it)
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印1到10但跳过5.
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
nums.forEach {
if (it == 5) return@forEach
println(it)
}
}
Run Code Online (Sandbox Code Playgroud)
在Kotlin游乐场试试吧.
Yoa*_*erg 34
编辑:
根据Kotlin的旧文档 - 链接被破坏,应该可以使用注释.但是,它尚未实施.
尚未实现自定义控件结构的中断和继续
此功能的问题是3年,所以我想它不会被修复.
原始答案:
由于你提供了一个(Int) -> Unit,你不能打破它,因为编译器不知道它是在一个循环中使用.
你有几个选择:
使用常规for循环:
for (index in 0 until times) {
// your code here
}
Run Code Online (Sandbox Code Playgroud)
如果循环是方法中的最后一个代码,则
可以return用来退出方法(或者return value如果它不是unit方法).
使用方法
创建返回Boolean以继续的自定义重复方法方法.
public inline fun repeatUntil(times: Int, body: (Int) -> Boolean) {
for (index in 0..times - 1) {
if (!body(index)) break
}
}
Run Code Online (Sandbox Code Playgroud)
Ray*_*aga 21
可以使用以下方法实现休息:
//Will produce"12 done with nested loop"
//Using "run" and a tag will prevent the loop from running again. Using return@forEach if I>=3 may look simpler, but it will keep running the loop and checking if i>=3 for values >=3 which is a waste of time.
fun foo() {
run loop@{
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@loop // non-local return from the lambda passed to run
print(it)
}
}
print(" done with nested loop")
}
Run Code Online (Sandbox Code Playgroud)
并且可以通过以下方式实现继续:
//Will produce: "1245 done with implicit label"
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@forEach // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with implicit label")
}
Run Code Online (Sandbox Code Playgroud)
正如任何人在这里建议的......阅读文档:P https://kotlinlang.org/docs/reference/returns.html#return-at-labels
ces*_*rds 15
正如Kotlin文档所说,使用return是可行的方法.关于kotlin的好处是,如果你有嵌套函数,你可以使用标签来表达你的返回来自哪里:
功能范围返回
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return // non-local return directly to the caller of foo()
print(it)
}
println("this point is unreachable")
}
Run Code Online (Sandbox Code Playgroud)
和 本地回报(它不会停止通过forEach = continuation)
fun foo() {
listOf(1, 2, 3, 4, 5).forEach lit@{
if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with explicit label")
}
Run Code Online (Sandbox Code Playgroud)
查看文档,这真的很棒:)
我对此有完美的解决方案(:
list.apply{ forEach{ item ->
if (willContinue(item)) return@forEach
if (willBreak(item)) return@apply
}}
Run Code Online (Sandbox Code Playgroud)
continue 输入行为 forEach
list.forEach { item -> // here forEach give you data item and you can use it
if () {
// your code
return@forEach // Same as continue
}
// your code
}
Run Code Online (Sandbox Code Playgroud)
对于break类型行为,您必须使用for until
for (index in 0 until list.size) {
val item = listOfItems[index] // you can use data item now
if () {
// your code
break
}
// your code
}
Run Code Online (Sandbox Code Playgroud)