lvlv
lvlv

Reputation: 11

Could I combine two closures in groovy?

I have one closure:

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

There is a get method to get the closure:

def getClosure() {
    check
}

I am trying to get the Logical disjunction of two calls (name could be "Dog" or "Cat"):

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);

My question is is there a way to create a c3 closure?

Thanks!

Upvotes: 1

Views: 1126

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

I don't think that Groovy ships with a combinator for or'ing predicates (if that's what you are asking). You'll probably have to do c3 = { c1(it) || c2(it) }.

Upvotes: 3

Related Questions