Groovy find and findAll take the result of the passed in closure and use groovy truth to either find the first element in the collection/map or filter the collection/map.
There are many situations situations where what you actually want to find or collect is the result of the closure, rather than test original elements and then reprocessing them all again.
The attached patch (with javadocs & unit tests) adds 2 methods to groovy objects, collections, and maps; enhanced versions of the existing groovy find and findAll methods:
findResult and findAllresults are valuable for shortening/clarifying code in many situations, but they're the most valuable when the closure calls a relatively expensive method (or one that isn't idempotent). When calling the method again with the results of a find/findAll is impractical.
I had a discussion today on twitter with Robert Fletcher (http://twitter.com/rfletcherEW/status/16410798707) that spurred this patch, though there have been many times in the past that I've wanted this functionality and ended up writing the 4+ lines of boilerplate code to work around it being missing.
The object/collection version of findAll iterates through the object and passes each value to the closure. The first closure result that is non-null (including false) will short circuit evaluation and return the value, example:
The map version of findResult is slightly different as what it returns is a Map.Entry with the key equal to the key of the map entry that generated a non-null value from the closure:
findAllResults will spin through the whole iterator, but will collect only those results from the closure that are non-null, so it can both collect and filter in one step:
The map version returns a map that contains the keys that had a non-null result with the result of the closure:
Other groovy iterators (like collect and inject) are not able to easily fulfill this role. Collect will not do any filtering, but instead returns a full list with nulls that need to be removed. Inject can sort of be made to fill this role, but it's often more work than it's worth as you need to manage the list creation/appending and the additional closure parameter. There isn't an equivalent of collect/inject on Maps.
I'd love it if this could be added to 1.7.4, but 1.8 would work too .
|Field||Original Value||New Value|
|Attachment||findResult_with_defaultResult.patch [ 49685 ]|
|Attachment||findResult_and_findAllResults.patch [ 49660 ]|
|Fix Version/s||1.8-beta-1 [ 16013 ]|
|Fix Version/s||1.7.4 [ 16563 ]|
|Fix Version/s||1.8-beta-2 [ 16629 ]|
|Fix Version/s||1.7.5 [ 16639 ]|
|Assignee||Guillaume Laforge [ guillaume ]|
|Resolution||Fixed [ 1 ]|
|Status||Open [ 1 ]||Resolved [ 5 ]|
|Summary||findResult and findAllResults object/collection/map enhancement patch||findResult object/collection/map enhancement patch|
|Status||Resolved [ 5 ]||Closed [ 6 ]|
|Project Import||Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]|
|Workflow||jira [ 12733196 ]||Default workflow, editable Closed status [ 12745038 ]|
|Flags||Patch [ 10430 ]|
|Patch Submitted||Yes [ 10763 ]|
|Project Import||Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]|
|Workflow||jira [ 12970848 ]||Default workflow, editable Closed status [ 12978612 ]|