Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Plans to allow breaking in a closure?

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • 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?
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic