Cannot breakout of foreach loop in scala

依然范特西╮ 提交于 2021-01-29 08:22:42

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/37520164/cannot-breakout-of-foreach-loop-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!