简体   繁体   English

Groovy findAll闭包参数

[英]Groovy findAll closure parameters

I want use groovy findAll with my param to filtering closure 我想用我的param使用groovy findAll来过滤闭包

filterClosure = { it, param ->
  it.getParam == param
}

How can I now call this closure in findAll? 我现在如何在findAll中调用此闭包? Like below? 如下?

myColl = someColl.findAll(filterClosure ??? )

Assuming your collection was a list, you could use curry to populate the extra closure parameter with your object: 假设您的集合是一个列表,您可以使用curry用您的对象填充额外的closure参数:

def someColl = ["foo", "bar", "foo", "baz", "foo"]

def filterClosure = { it, param -> it.getParam == param }

myColl = someColl.findAll(filterClosure.curry([getParam:'foo']))

assert ["foo", "foo", "foo"] == myColl

In the code above, the filterClosure "it" will be assigned what is passed to curry as a parameter and "param" is passed a collection item from findAll. 在上面的代码中,filterClosure“it”将被分配给作为参数传递给curry的内容,并且“param”从findAll传递给一个集合项。 This wouldn't work for a Map collection since findAll for it takes a closure with either one or two parameters. 这对于Map集合不起作用,因为findAll for它需要一个带有一个或两个参数的闭包。

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

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