Groovy findAll 闭包参数

发布于 2024-08-03 20:24:11 字数 245 浏览 3 评论 0原文

我想使用 groovy findAll 和我的参数来过滤闭包

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

现在我如何在 findAll 中调用这个闭包?像下面这样?

myColl = someColl.findAll(filterClosure ??? )

I want use groovy findAll with my param to filtering closure

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

How can I now call this closure in findAll? Like below?

myColl = someColl.findAll(filterClosure ??? )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-08-10 20:24:11

假设你的集合是一个列表,你可以使用 curry 用你的对象填充额外的闭包参数:

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

在上面的代码中,filterClosure“it”将被分配传递给 curry 作为参数的内容,而“param”将被传递一个集合来自 findAll 的项目。这对于 Map 集合不起作用,因为 findAll 需要一个带有一个或两个参数的闭包。

Assuming your collection was a list, you could use curry to populate the extra closure parameter with your object:

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. This wouldn't work for a Map collection since findAll for it takes a closure with either one or two parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文