Groovy:如何使用闭包变量而不是 NOT 将过滤器应用于集合?

Groovy: how to apply filters using closure variable and NOT to a collection?

提问人:eastwater 提问时间:10/11/2022 最后编辑:eastwater 更新时间:10/11/2022 访问量:65

问:

Groovy:如何使用定义的闭包将过滤器应用于集合?例如:

def f = { it -> it.name.contains("foo") }

aCollection.filter { it -> !it.name.contains("foo") }

如何将闭包变量应用于过滤方法?喜欢

aCollection.filter {! f }  //NOT
过滤器 时髦 的瓶盖

评论

1赞 emilles 10/11/2022
aCollection的类型是什么?过滤器的签名是什么?您可以将 f 作为参数传递:aCollection.filter(f)。如果要否定 f: aCollection.filter{ !f(it) } 的结果
1赞 eastwater 10/11/2022
谢谢。刚刚想通了,调用闭包类似于调用方法。

答: 暂无答案