无法在Scala中突破foreach循环 [英] Cannot breakout of foreach loop in scala

查看:56
本文介绍了无法在Scala中突破foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

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

我想突破:

> 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

基本上我想在我的第一个条件满足时从循环中跳出来.所以我想按照scala教程使用下面的代码,但不知道将breakable命令放在哪里,因为它给我带来错误.

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确实没有提供任何易于使用的 break / continue 控制流原语.这不是做事的功能方式.

Scala doesn't really offer any easy-to-use break/continue control flow primitives. It's not the functional way of doing things.

Scala集合中可用的大多数方法,例如 foreach ,都旨在检查/修改整个集合.例外包括:包含对应存在 forall indexWhere 等您会注意到,大多数(全部?)都将布尔值作为参数(谓词函数)或返回类型来处理.

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.

如果您的算法无法重新利用这些延迟评估的方法之一,那么我建议您遵循@ pietro909的建议并将其重新设计为一个递归函数,该函数在每次调用/递归时测试一个或多个退出条件.

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.

我知道这并不是您真正想要的,确实可以通过在代码中插入 breakable 块来实现所需的功能,但是如果您

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

注意: break 不仅终止 foreach()语句,它还突破了整个 breakable 块.

Note: The break doesn't just terminate the foreach() statement, it breaks out of the entire breakable block.

这篇关于无法在Scala中突破foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆