67

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:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155

7 Answers7

65

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 }
MariuszS
  • 30,646
  • 12
  • 114
  • 155
Steve Sowerby
  • 1,136
  • 1
  • 7
  • 7
  • Could not understand code of the usage, could you please eloborate? My understand is that everything after loop is considered as closure, and how until is called? – Rao Sep 02 '15 at 06:14
  • 3
    @Rao Think of it as `loop({...}).until({...})`. So `until()` is called on the return value of `loop()`. Groovy command expressions mean you can leave out the `.` in front of `until()`. – Peter Ledbrook Oct 20 '15 at 16:31
  • @PeterLedbrook, thank you so much for your explanation. It is clean now. – Rao Oct 22 '15 at 17:06
38

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?

topr
  • 4,482
  • 3
  • 28
  • 35
  • 2
    It has quite an unfortunate amount of brackets and braces but is the most elegant way anyway, without any repetitions. – topr Sep 28 '17 at 17:32
  • 1
    Though I like the elegance and cleverness of this solution, I'm not happy with the readability. For myself I could imagine coming back to this code after a year and thinking "what is happening here". I'm undecided, because I don't like repetition either. – rudolfson Nov 13 '19 at 08:28
30

Depending on your use case, there are options like this: do .. while() in Groovy with inputStream?

Or you can do:

x.doIt()
while( !x.finished ) { x.doIt() }

Or

while( true ) {
    x.doIt()
    if( x.finished ) break
}
Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
10

You can use a condition variable with the regular while loop:

def keepGoing = true
while( keepGoing ){
    doSomething()
    keepGoing = ... // evaluate the loop condition here
}
GreenGiant
  • 4,930
  • 1
  • 46
  • 76
10

Update Groovy 2.6 has been abandoned to concentrate on 3.0.

From Groovy 2.6 on, do-while is supported when enabling the new Parrot Parser, from Groovy 3.0 on this is the default. See release notes:

// classic Java-style do..while loop
def count = 5
def fact = 1
do {
    fact *= count--
} while(count > 1)
assert fact == 120
Mene
  • 3,739
  • 21
  • 40
3

By now, Groovy has support for do/while:

do {

  x.doIt()

} while (!x.isFinished())
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
1

Or you can implement it in a Groovier way :

def loop(Closure g){
    def valueHolder = [:]
    g.delegate = valueHolder
    g.resolveStrategy = Closure.DELEGATE_FIRST
    g()
    [until:{Closure w ->
        w.delegate = valueHolder
        w.resolveStrategy = Closure.DELEGATE_FIRST
        while(!w()){
        g()
        }
        }]
}
Cao Cao
  • 19
  • 2