从Kotlin中的“buildSequence”返回
我正在使用Kotlin中的从Kotlin中的“buildSequence”返回,kotlin,coroutine,Kotlin,Coroutine,我正在使用Kotlin中的buildSequence函数。如何在函数中间结束迭代?我正在寻找类似于C#的收益率突破语句的东西 我的代码如下所示。我被困在待办事项上了 fun foo(list:List<Number>): Sequence<Number> = buildSequence { if (someCondition) { // TODO: Bail out early with an empty sequence // r
buildSequence
函数。如何在函数中间结束迭代?我正在寻找类似于C#的收益率突破
语句的东西
我的代码如下所示。我被困在待办事项上了
fun foo(list:List<Number>): Sequence<Number> = buildSequence {
if (someCondition) {
// TODO: Bail out early with an empty sequence
// return doesn't seem to work....
}
list.forEach {
yield(someProcessing(it))
}
}
编辑2
问题是,我将
返回
放在一个本地助手函数中,该函数在构建序列
中的多个点验证数据(因此是助手函数)。显然,我无法从helper函数中的buildSequence
返回。错误消息没有太大帮助…只需使用return@buildSequence
,它是一个,而未标记的返回将表示“从函数foo
返回”
另请参见:只需使用return@buildSequence
,它是一个,而未标记的返回将表示“从函数foo
返回”
另请参见:,因为Kotlin v 1.3.x首选序列语法已更改。(buildSequence
替换为kotlin.sequences.sequence
)
更新了post 1.3.x Kotlin的“从生成器提前返回”代码段(包括try catch
和==null
提前返回示例):
// gen@ is just a subjective name i gave to the code block.
// could be `anything@` you want
// Use of named returns prevents "'return' is not allowed here" errors.
private fun getItems() = sequence<Item> gen@ {
val cursor: Cursor?
try {
cursor = contentResolver.query(uri,*args)
} catch (e: SecurityException) {
Log.w(APP_NAME, "Permission is not granted.")
return@gen
}
if (cursor == null) {
Log.w(APP_NAME, "Query returned nothing.")
return@gen
}
// `.use` auto-closes Closeable. recommend.
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
cursor.use {
// iterate over cursor to step through the yielded records
while (cursor.moveToNext()) {
yield(Item.Factory.fromCursor(cursor))
}
}
}
//gen@只是我给代码块起的一个主观名称。
//可以是你想要的任何东西
//使用命名返回可防止出现“此处不允许返回”错误。
private fun getItems()=序列gen@{
瓦尔光标:光标?
试一试{
cursor=contentResolver.query(uri,*args)
}捕获(e:SecurityException){
Log.w(应用程序名称,“未授予权限”)
return@gen
}
if(游标==null){
Log.w(应用程序名称,“查询未返回任何内容”)
return@gen
}
//`。使用`自动关闭可关闭。建议。
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
游标。使用{
//在游标上迭代以单步遍历生成的记录
while(cursor.moveToNext()){
产量(项目.工厂.从游标(游标))
}
}
}
(之前所有帮助我登上“命名返回”轨道的帖子的Thx。)因为Kotlin v 1.3.x首选序列语法发生了变化。(buildSequence
替换为kotlin.sequences.sequence
)
更新了post 1.3.x Kotlin的“从生成器提前返回”代码段(包括try catch
和==null
提前返回示例):
// gen@ is just a subjective name i gave to the code block.
// could be `anything@` you want
// Use of named returns prevents "'return' is not allowed here" errors.
private fun getItems() = sequence<Item> gen@ {
val cursor: Cursor?
try {
cursor = contentResolver.query(uri,*args)
} catch (e: SecurityException) {
Log.w(APP_NAME, "Permission is not granted.")
return@gen
}
if (cursor == null) {
Log.w(APP_NAME, "Query returned nothing.")
return@gen
}
// `.use` auto-closes Closeable. recommend.
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
cursor.use {
// iterate over cursor to step through the yielded records
while (cursor.moveToNext()) {
yield(Item.Factory.fromCursor(cursor))
}
}
}
//gen@只是我给代码块起的一个主观名称。
//可以是你想要的任何东西
//使用命名返回可防止出现“此处不允许返回”错误。
private fun getItems()=序列gen@{
瓦尔光标:光标?
试一试{
cursor=contentResolver.query(uri,*args)
}捕获(e:SecurityException){
Log.w(应用程序名称,“未授予权限”)
return@gen
}
if(游标==null){
Log.w(应用程序名称,“查询未返回任何内容”)
return@gen
}
//`。使用`自动关闭可关闭。建议。
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
游标。使用{
//在游标上迭代以单步遍历生成的记录
while(cursor.moveToNext()){
产量(项目.工厂.从游标(游标))
}
}
}
(Thx用于帮助我进入“命名返回”轨道的所有先前帖子。)IntelliJ继续报告“返回”在我尝试return@buildSequence
。这很奇怪,我可以在buildSequence
中使用返回来构建和运行此代码。Java8目标,Kotlin1.2.41。事实上,这里有一个可运行的示例:它应该可以正常工作。请尝试重建项目。如果它仍然存在,您可以复制它,请报告给。我找到了它,并在编辑中记录了结果。在最初的问题中,我戏剧性地过度简化了我的代码,把所有人都抛弃了。感谢您的帮助。IntelliJ继续报告“return”在我尝试return@buildSequence
。这很奇怪,我可以在buildSequence
中使用返回来构建和运行此代码。Java8目标,Kotlin1.2.41。事实上,这里有一个可运行的示例:它应该可以正常工作。请尝试重建项目。如果它仍然存在,您可以复制它,请报告给。我找到了它,并在编辑中记录了结果。在最初的问题中,我戏剧性地过度简化了我的代码,把所有人都抛弃了。感谢您的帮助。要实现中止序列生成的远程操作,您可以使用异常。从helper函数抛出一个验证异常,并使用一个try-catch包装整个buildSequence
块,该块只返回,而不让异常传播。True。事实上,我最初是抛出一个异常(并让它传播到构建序列
之外)。这是在将异常重构为空序列时出现的。来自.Net世界,我已经被灌输了“不要对常规控制流使用异常”。也许它在JVM上更容易接受……同样的警告也适用于JVM,但跨调用堆栈的远程信令是例外情况的原因。如果在同一个方法体中抛出并捕获异常,这将是一种滥用。要实现中止序列生成的长期操作,可以使用异常。从helper函数抛出一个验证异常,并使用一个try-catch包装整个buildSequence
块,该块只返回,而不让异常传播。True。事实上,我最初是抛出一个异常(并让它传播到构建序列
之外)。这是在将异常重构为空序列时出现的。来自.Net世界,我已经被灌输了“不要对常规控制流使用异常”。也许这在网上更容易接受