Best pattern for simulating “continue” in Groovy closure

前端 未结 6 1941
难免孤独
难免孤独 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:29

    Use return to continue and any closure to break.

    Example

    File content:

    1
    2
    ----------------------------
    3
    4
    5
    

    Groovy code:

    new FileReader('myfile.txt').any { line ->
        if (line =~ /-+/)
            return // continue
    
        println line
    
        if (line == "3")
            true // break
    }
    

    Output:

    1
    2
    3
    

提交回复
热议问题