Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way for do ... while in groovy

How to do code something like this in groovy?

do {    x.doIt()  } while (!x.isFinished()) 

Because there is no do ... while syntax in groovy.

No 'do ... while()' syntax as yet.

Due to ambiguity, we've not yet added support for do .. while to Groovy

References:

  • groovy - dev > do while
  • Migration From Classic to JSR syntax
  • Groovy Documentation > Control Structures > Looping
  • Rosetta Code > Loops/Do-while Groovy
like image 755
MariuszS Avatar asked Jan 04 '14 13:01

MariuszS


People also ask

Do While statements Groovy?

Groovy - While StatementThe while statement is executed by first evaluating the condition expression (a Boolean value), and if the result is true, then the statements in the while loop are executed. The process is repeated starting from the evaluation of the condition in the while statement.

How do you break a while loop in Groovy?

The break statement can also be used with while and for statements. Executing a break statement with any of these looping constructs causes immediate termination of the innermost enclosing loop.

How do you write a loop in Groovy?

The for-in statement is used to iterate through a set of values. The for-in statement is generally used in the following way. for(variable in range) { statement #1 statement #2 … }

Why is Groovy looping?

Looping is an essential feature in every Programming Language including Groovy. Loops a control flow statement for traversing or iterating items in a collection, array, list, set, etc. There's a lot of ways to loop through collection, array, list, or set in Groovy.


2 Answers

You can roll your own looping that's almost what you want. Here's an example with loop { code } until { condition } You can't have a corresponding loop { code } while { condition } because while is a keyword. But you could call it something else.

Anyway here's some rough and ready code for loop until. One gotcha is you need to use braces for the until condition to make it a closure. There may well be other issues with it.

class Looper {    private Closure code     static Looper loop( Closure code ) {       new Looper(code:code)    }     void until( Closure test ) {       code()       while (!test()) {          code()       }    } } 

Usage:

import static Looper.*  int i = 0 loop {    println("Looping : "  + i)    i += 1 } until { i == 5 } 
like image 61
Steve Sowerby Avatar answered Sep 19 '22 12:09

Steve Sowerby


So many answers and not a single one without a redundant call, a shame ;)

This is the closest it can get to purely language syntax based do-while in Groovy:

while ({     x.doIt()     !x.isFinished() }()) continue 

The last statement within curly braces (within closure) is evaluated as a loop exit condition.

Instead of continue keyword a semicolon can be used.

Additional nice thing about it, loop can be parametrized (kind of), like:

Closure<Boolean> somethingToDo = { foo ->     foo.doIt()     !foo.isFinished() } 

and then elsewhere:

while (somethingToDo(x)) continue 

Formerly I've proposed this answer over here: How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?

like image 42
topr Avatar answered Sep 19 '22 12:09

topr