【问题标题】:Could I combine two closures in groovy?我可以在 groovy 中结合两个闭包吗?
【发布时间】:2014-12-26 20:56:32
【问题描述】:

我有一个闭包:

def Boolean check(String name, String value, Context c){
   c.name.equalsIgnoreCase(value);
}    

有获取闭包的get方法:

def getClosure() {
    check
}

我正在尝试获取两个呼叫的逻辑析取(名称可能是“狗”或“猫”):

c1 = check.curry("name", "Dog");
c2 = check.curry("name", "Cat");

c3 = c1 || c2; /*Could this closure be created?*/

Context ctx = new Context();
c3.call(ctx);

我的问题是有没有办法创建一个 c3 闭包?

谢谢!

【问题讨论】:

  • 你不能使用番石榴谓词吗?当您正在缩减为单个参数闭包时,它们应该只使用 groovy as 干净地映射
  • c3.call(ctx) 应该返回['Dog', 'Cat']['Dog']['Cat']

标签: groovy closures


【解决方案1】:

我不认为 Groovy 附带用于 or'ing 谓词的组合器(如果这是您要问的)。你可能不得不做c3 = { c1(it) || c2(it) }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    相关资源
    最近更新 更多