Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy findAll closure parameters

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 ??? )
like image 999
Alexey Sviridov Avatar asked Sep 07 '09 05:09

Alexey Sviridov


1 Answers

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.

like image 64
John Wagenleitner Avatar answered Sep 23 '22 08:09

John Wagenleitner