简体   繁体   中英

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.

> 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. 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. 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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