Best pattern for simulating “continue” in Groovy closure

前端 未结 6 1978
难免孤独
难免孤独 2020-12-08 01:54

It seems that Groovy does not support break and continue from within a closure. What is the best way to simulate this?

revs.eachLin         


        
6条回答
  •  清歌不尽
    2020-12-08 02:14

    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}
    

提交回复
热议问题