简体   繁体   English

无法在 Scala 中突破 foreach 循环

[英]Cannot breakout of foreach loop in scala

i have this below code我有下面的代码

> .foreach("${plist}", "newshole") {
>        exec(
>          http("get the user id")
>            .get("${newshole}/jcr:content.1.json")
>            .headers(headers_2)
>            .check(bodyString.saveAs("Res1"))
>        )
>        exec(session => {
>          var mynewshole = session("Res1").as[String]
>          if (!mynewshole.contains("testingInProgress")) {
>            println("Doesn't contain: " + mynewshole)
>            (http("post the user id")
>              .post("${newshole}/jcr:content")
>              .headers(headers_2)
>              .formParam("testingInProgress", session.userId))
>            exec(http("Create print package")
>              .post("/bin/cqtg-create-print-package.do")
>              .headers(headers_2)
>              .formParam("newsholeId", "${plist}")
>              .formParam("digitalMasterId", "1adpy8")
>              .check(status.is(200)))
> 
>          }
>          session
>        })   
>      }

i want to break out of :我想摆脱:

> if (!mynewshole.contains("testingInProgress")) {
>            println("Doesn't contain: " + mynewshole)
>            (http("post the user id")
>              .post("${newshole}/jcr:content")
>              .headers(headers_2)
>              .formParam("testingInProgress", session.userId))
>            exec(http("Create print package")
>              .post("/bin/cqtg-create-print-package.do")
>              .headers(headers_2)
>              .formParam("newsholeId", "${plist}")
>              .formParam("digitalMasterId", "1adpy8")
>              .check(status.is(200)))
> 
>          }
>          session

basically i want break out from the loop when my first condition meet.So i want to use the below code as per scala tutorials but don't know where to put the breakable command as it is giving me errors.基本上我想在我的第一个条件满足时从循环中跳出。所以我想按照 scala 教程使用下面的代码,但不知道在哪里放置可破解命令,因为它给了我错误。

> breakable{
>             code ()
>          break;
>            }

but don't know where to put it.Any Idea????但不知道把它放在哪里。任何想法????

Scala doesn't really offer any easy-to-use break / continue control flow primitives. Scala 并没有真正提供任何易于使用的break / continue控制流原语。 It's not the functional way of doing things.这不是做事的功能方式。

Most of the methods available on Scala collections, like foreach , are designed to inspect/modify the entire collection. Scala 集合上可用的大多数方法,如foreach ,旨在检查/修改整个集合。 The exceptions include: contains , corresponds , exists , forall , indexWhere , etc. You'll note that most (all?) deal with Booleans, either as an argument (a predicate function) or as the return type.例外情况包括: containscorrespondsexistsforallindexWhere等,你会注意到,大多数(所有?)处理布尔值,无论是作为一个参数(谓语功能)或作为返回类型。

If your algorithm can't be reworked to utilize one of these lazy-evaluated methods then I'd recommend following @pietro909's advice and redesign it as a recursive function which tests for one or more exit conditions on every invocation/recursion.如果您的算法无法重新设计以使用这些惰性评估方法之一,那么我建议您遵循 @pietro909 的建议并将其重新设计为递归函数,该函数在每次调用/递归时测试一个或多个退出条件。

I know that isn't really what you asked for, and it's true you can achieve what you want by inserting a breakable block in your code, but if you inspect the source you'll see that the breaks are implemented by throwing/catching exceptions, which is pretty inefficient and usually worth avoiding.我知道这是不是真的你要的,你可以通过将实现你想要的这是真的breakable在你的代码块,但是如果你查看源代码,你会看到符抛出/捕获异常实现,这是非常低效的,通常值得避免。

But if you're determined to go down that road, this should provide some guidance:但是,如果您决心走这条路,这应该会提供一些指导:

scala> import util.control.Breaks
import util.control.Breaks

scala> val mybreaks = new Breaks
mybreaks: scala.util.control.Breaks = scala.util.control.Breaks@69ea3742

scala> import mybreaks.{break, breakable}
import mybreaks.{break, breakable}

scala> breakable {
     | (1 to 34).foreach(x => if (x > 9) break else println(x))
     | println("all done")
     | }
1
2
3
4
5
6
7
8
9

Note: The break doesn't just terminate the foreach() statement, it breaks out of the entire breakable block.注意: break不仅仅终止foreach()语句,它打破了整个breakable块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM