LoginSignup
3
3

More than 5 years have passed since last update.

GroovyのClosureを途中で止める

Last updated at Posted at 2014-04-01

たまに途中で止めたくなるClosureの止め方。
Javaのループ感覚でbreakしても止まらない。

↓ 1,000までのリストを888で止める場合

(1..1000).each {
    if (it > 888) return true
    println it
}

追記(2014/4/5)

ただ、この方法だと1000までは評価しているから厳密には止まってなくて、処理をしていないだけ。ホントの意味で止める方法はあるんだろうか?

ちなみに、continueするのも同じような方法でできる。

↓888の時は処理しないで次に飛ぶ。

(1..1000).each {
    if (it == 888) return true
    println it
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3