简体   繁体   English

闭包中的Groovy函数调用

[英]Groovy function call in a closure

How can I make a function call in a closure in Groovy? 如何在Groovy的闭包中进行函数调用? Currently trying this but it results in all iterations using the values from the last array element: 当前正在尝试此操作,但它使用最后一个数组元素中的值进行所有迭代:

def branches = [:]
for (int i = 0; i < data.steps.size(); i++) {
    branches["${data.steps.get(i).name}"] = {
        myFunc(data.steps.get(i))
    }
}
parallel branches

That's a common gotcha 这是一个常见的陷阱

This should work: 这应该工作:

def branches = data.steps.collectEntries { step ->
    [step.name, { myFunc(step) }]
}
parallel branches

Or 要么

def branches = data.steps.inject([:]) { map, step ->
    map << [(step.name): { myFunc(step) }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM