• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Plans to allow breaking in a closure?

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any plans to allow a way a "break" from an each closure? In other words, sometimes I'd like to do ..

collection.eachWithIndex{ it, i ->
//do stuff with it
if ( someCalculation == i ) break

}

But there is no way that I see to break out of an each closure?
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just put the rest of the code in a code block to mimic this behavior. Isn't that what Java does under the hood anyway with a break?




--Mark
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Herschberg:
You can just put the rest of the code in a code block to mimic this behavior. Isn't that what Java does under the hood anyway with a break?




--Mark



How would that help with an each closure? or are you suggesting you wouldn't use an each closure at all - if that's the case, I think that's sort of limiting.

If your suggesting use a closure within the each closure, I'm not sure how that would help?

I'm sure I'm missing what you're suggesting.
 
Author
Posts: 135
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick, how about this:

println "Print all values in collection"
('a'..'f').each { println it }

println "To print until 'd'"

('a'..'f').find { println it
it >= 'd'
}

I have used find in cases where I want to break out of each.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Herschberg:
You can just put the rest of the code in a code block to mimic this behavior. Isn't that what Java does under the hood anyway with a break?




--Mark



In this example <some code> will execute for every element, while <other code> will not. The point of a break in a loop (as opposed to continue) is that not only do you stop execution of the code for the current element, but you also stop the loop/each for all subsequent elements. So you would need the condition twice:

The duplicated condition check could be eliminated if the intended break occurs at the beginning or end of the code block.

To me, a break statement would look cleaner, and also avoid the need to repeatedly recheck the condition after the break had occurred. Usually this is not too big a deal, but for large collections the effect may be significant.

Venkat's example nicely avoids the repeated checks to to condition. However it is a bit less readable than might be desired, on first glance. And it's a bit more work to handle a break from the middle of the code:

For what it's worth, Ruby does allow break from within a closure, which terminates the controlling statement (e.g. each()). Meanwhile the BGGA proposal for closures in Java has very different consequences for break (as well as continue and return) which would not mesh well with this usage.
[ April 09, 2008: Message edited by: Jim Yingst ]
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic