Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify groovy loop code

I have an groove code (with some java style elements)

dates.forEach new Consumer<Period>() {
    @Override
    void accept(Period period) {
        println period
    }
}

Is it possible to do it simpler?

I would like to have something like

dates.forEach println

or

dates.forEach println date
like image 452
Sergii Avatar asked Jul 17 '17 13:07

Sergii


People also ask

How do you write a loop in groovy?

Groovy Programming Fundamentals for Java Developers S.No. The 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 for statement is used to iterate through a set of values.

What is loop groovy?

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.


1 Answers

Just use

dates.each { println it }
like image 93
tim_yates Avatar answered Sep 30 '22 19:09

tim_yates