Jay*_*ard 44 loops functional-programming continue break kotlin
在科特林,我不能做一个break
或continue
一个功能回路和我的拉姆达内-就像我可以从一个正常的for
循环.例如,这不起作用:
(1..5).forEach {
continue@forEach // not allowed, nor break@forEach
}
Run Code Online (Sandbox Code Playgroud)
有旧的文档提到这个可用,但它似乎从未实现过.当我想要continue
或break
来自lambda 时,获得相同行为的最佳方法是什么?
注意: 这个问题是由作者故意编写和回答的(自答案问题),因此对于常见问题的Kotlin主题的惯用答案存在于SO中.还要澄清为Kotlin的alphas写的一些非常古老的答案,这些答案对于当前的Kotlin来说是不准确的.
Jay*_*ard 93
除了您要求提供类似功能之外,还有其他选项.例如:
您可以使用filter
以下方法避免处理某些值:( 如acontinue
)
dataSet.filter { it % 2 == 0 }.forEach {
// do work on even numbers
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令停止功能循环takeWhile
:( 如abreak
)
dataSet.takeWhile { it < 10 }.forEach {
// do work on numbers as long as they are < 10, otherwise stop
}
Run Code Online (Sandbox Code Playgroud)
一个更复杂的,虽然荒谬的例子,你想要做一些处理,跳过一些结果值,然后停在一组不同的条件,将是:
dataSet.asSequence()
.takeWhile { it >= 0 } // a -1 signals end of the dataset (break)
.map { it + 1 } // increment each number
.filterNot { it % 5 == 0 } // skip (continue) numbers divisible by 5
.map { it - 1 } // decrement each number by 1
.filter { it < 100 } // skip (continue) if number is >= 100
.drop(5) // ignore the first 5 numbers
.take(10) // use the next 10 numbers and end
.forEach {
// do work on the final list
}
Run Code Online (Sandbox Code Playgroud)
这些功能的组合往往不需要continue
或break
.这里有无穷无尽的不同选择,而且可以记录下来.为了了解可以做什么,最好是学习Kotlin标准库中可用于集合,延迟序列和可迭代的所有函数.
有时,在某些情况下,您仍然需要变异状态,break
或者continue
在功能模型中很难做到.你可以使用更复杂的函数来使它工作fold
,reduce
并与函数结合使用filter
,takeWhile
但有时候更难以理解.因此,如果您真的想要这种确切的行为,您可以使用lambda表达式返回,该表达式模仿continue
或break
取决于您的用法.
这是一个模仿的例子continue
:
(1..5).forEach {
if (it == 3) return@forEach // mimic continue@forEach
// ... do something more
}
Run Code Online (Sandbox Code Playgroud)
当您遇到嵌套或混乱的情况时,您可以更复杂并使用标签:
(1..3).forEach outer@ { x ->
(1..3).forEach inner@ { y ->
if (x == 2 && y == 2) return@outer // mimic continue@outer
if (x == 1 && y == 1) return@inner // mimic continue@inner
// ... do something more
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想做一个break
你需要的东西,你可以返回,我们将在这里使用该run()
功能来帮助我们:
run breaker@ {
(1..20).forEach { x ->
if (x == 5) return@breaker // mimic break@forEach
// ... do something more
}
}
Run Code Online (Sandbox Code Playgroud)
而不是run()
它可能是let()
或apply()
任何自然的你周围的forEach
那个是你想要打破的地方.但是你也会在同一个块之后跳过代码,forEach
所以要小心.
这些是内联函数,所以实际上它们并没有真正增加开销.
阅读所有特殊情况(包括匿名函数)的返回和跳转的Kotlin参考文档.
这是一个单元测试,证明这一切都有效:
@Test fun testSo32540947() {
val results = arrayListOf<Pair<Int,Int>>()
(1..3).forEach outer@ { x ->
(1..3).forEach inner@ { y ->
if (x == 2 && y == 2) return@outer // continue @outer
if (x == 1 && y == 1) return@inner // continue @inner
results.add(Pair(x,y))
}
}
assertEquals(listOf(Pair(1,2), Pair(1,3), Pair(2,1), Pair(3,1), Pair(3,2), Pair(3,3)), results)
val results2 = arrayListOf<Int>()
run breaker@ {
(1..20).forEach { x ->
if (x == 5) return@breaker
results2.add(x)
}
}
assertEquals(listOf(1,2,3,4), results2)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16319 次 |
最近记录: |