Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EachWithIndex groovy statement

Tags:

groovy

I am new to groovy and I've been facing some issues understanding the each{} and eachwithindex{} statements in groovy.

Are each and eachWithIndex actually methods? If so what are the arguments that they take?

In the groovy documentation there is this certain example:

def numbers = [ 5, 7, 9, 12 ] numbers.eachWithIndex{ num, idx -> println "$idx: $num" } //prints each index and number 

Well, I see that numbers is an array. What are num and idx in the above statement? What does the -> operator do?

I do know that $idx and $num prints the value, but how is it that idx and num are automatically being associated with the index and contents of the array? What is the logic behind this? Please help.

like image 879
Vamsi Emani Avatar asked Apr 08 '11 06:04

Vamsi Emani


People also ask

How do I write a print statement in Groovy?

You can use the print function to print a string to the screen. You can include \n to embed a newline character. There is no need for semi-colon ; at the end of the statement. Alternatively you can use the println function that will automatically append a newline to the end of the output.

How do you get the index of an element in a list in Groovy?

Groovy - indexOf() Returns the index within this String of the first occurrence of the specified substring. This method has 4 different variants. public int indexOf(int ch) − Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.

How do I iterate a map in groovy?

We can iterate through entries using the each() and eachWithIndex() methods. The each() method provides implicit parameters, like entry, key, and value, which correspond to the current Entry. The eachWithIndex() method also provides an index in addition to Entry. Both the methods accept a Closure as an argument.

What is Groovy expression?

Groovy Programming Fundamentals for Java Developers A regular expression is a pattern that is used to find substrings in text. Groovy supports regular expressions natively using the ~”regex” expression. The text enclosed within the quotations represent the expression for comparison.


1 Answers

These are plain methods but they follow quite a specific pattern - they take a Closure as their last argument. A Closure is a piece of functionality that you can pass around and call when applicable.

For example, method eachWithIndex might look like this (roughly):

void eachWithIndex(Closure operation) {     for (int i = 0; this.hasNext(); i++) {         operation(this.next(), i); // Here closure passed as parameter is being called     } } 

This approach allows one to build generic algorithms (like iteration over items) and change the concrete processing logic at runtime by passing different closures.

Regarding the parameters part, as you see in the example above we call the closure (operation) with two parameters - the current element and current index. This means that the eachWithIndex method expects to receive not just any closure but one which would accept these two parameters. From a syntax prospective one defines the parameters during closure definition like this:

{ elem, index ->     // logic  } 

So -> is used to separate arguments part of closure definition from its logic. When a closure takes only one argument, its parameter definition can be omitted and then the parameter will be accessible within the closure's scope with the name it (implicit name for the first argument). For example:

[1,2,3].each {     println it }  

It could be rewritten like this:

[1,2,3].each({ elem ->     println elem }) 

As you see the Groovy language adds some syntax sugar to make such constructions look prettier.

like image 187
oiavorskyi Avatar answered Oct 16 '22 11:10

oiavorskyi