wood burning stoves 2.0*
The moose likes Other Languages and the fly likes Plans to allow breaking in a closure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Plans to allow breaking in a closure?" Watch "Plans to allow breaking in a closure?" New topic
Author

Plans to allow breaking in a closure?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Dec 04, 2000
Posts: 6037
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

Joined: Apr 03, 2001
Posts: 281
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

Joined: Jan 28, 2008
Posts: 79
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Plans to allow breaking in a closure?