I have a collection and I'm wanting to find certain elements and transform them. I can do this in two closures but I was wondering if it is possible with only one?
def c = [1, 2, 3, 4] def result = c.findAll { it % 2 == 0 } result = result.collect { it /= 2 }
My true use case is with Gradle, I want to find a specific bunch of files and transform them to their fully-qualified package name.
You can use findResults
:
def c = [1, 2, 3, 4] c.findResults { i -> i % 2 == 0 ? // if this is true i / 2 : // return this null // otherwise skip this one }
Also, you will get []
in case none of the elements satisfies the criteria (closure)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With