EachWithIndex groovy语句 [英] EachWithIndex groovy statement

查看:884
本文介绍了EachWithIndex groovy语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是groovy的新手,我在理解groovy中的each{}eachwithindex{}语句时遇到了一些问题.

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

eacheachWithIndex实际上是方法吗?如果是这样,他们采取什么论点?

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

在groovy文档中有以下示例:

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

好吧,我看到numbers是一个数组.上面的陈述中的numidx是什么? ->运算符做什么?

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

我确实知道$idx$num会打印值,但是idxnum如何自动与数组的索引和内容关联?这背后的逻辑是什么?请帮忙.

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.

推荐答案

这些是简单的方法,但是它们遵循特定的模式-它们以Closure作为最后一个参数.闭包是您可以传递并在适用时调用的一项功能.

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.

例如,方法eachWithIndex可能看起来像这样(大致):

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.

关于参数部分,如您在上面的示例中看到的,我们将闭包(operation)称为两个参数-当前元素和当前索引.这意味着eachWithIndex方法不仅希望接收任何闭包,而且希望接收这两个参数.从语法上的预期,可以在闭包定义期间定义参数,如下所示:

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 
}

因此,->用于将闭包定义的参数部分与其逻辑分开.当闭包仅接受一个参数时,可以省略其参数定义,然后可以在闭包的范围内使用名称it(第一个参数的隐式名称)访问该参数.例如:

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
} 

可以这样重写:

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

如您所见,Groovy语言添加了一些语法糖,以使这种构造看起来更漂亮.

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

这篇关于EachWithIndex groovy语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆