is it possible to break out of closure in groovy

后端 未结 9 1921
醉梦人生
醉梦人生 2020-12-09 08:33

is there a way to \'break\' out of a groovy closure.

maybe something like this:

[1, 2, 3].each { 
  println(it)
  if (it == 2)
    break 
}
<         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 08:53

    With rx-java you can transform an iterable in to an observable.

    Then you can replace continue with a filter and break with takeWhile

    Here is an example:

    import rx.Observable
    
    Observable.from(1..100000000000000000)
              .filter { it % 2 != 1} 
              .takeWhile { it<10 } 
              .forEach {println it}
    

提交回复
热议问题