You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/07/13 19:57:00 UTC
[jira] [Commented] (GROOVY-5053) Support for two-parameter closures in collect{} find{} each{} and etc accepting "it" and its index
[ https://issues.apache.org/jira/browse/GROOVY-5053?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17742937#comment-17742937 ]
Eric Milles commented on GROOVY-5053:
-------------------------------------
When the item type is a list (or tuple) you can use destructuring to get at the list elements with your closure parameters.
For example:
{code:groovy}
[ ['a',1], ['b',2] ].each { letter, number ->
// ...
}
{code}
Each list could have more than 2 elements and so you could have more than 2 params to capture them.
> Support for two-parameter closures in collect{} find{} each{} and etc accepting "it" and its index
> --------------------------------------------------------------------------------------------------
>
> Key: GROOVY-5053
> URL: https://issues.apache.org/jira/browse/GROOVY-5053
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-jdk
> Affects Versions: 1.8.2
> Reporter: Nikita Y Volkov
> Priority: Major
>
> This would be extremely useful, much more consistent and smarter than implementations like {{eachWithIndex}}. Whenever the user provides a two parameter closure to any of those {{Iterable}} extension methods, this would mean that the first parameter is the current item and the second is its index. If the closure has one parameter (or none), the current item gets passed. In all other cases it should throw an exception. A simple and very productive convention.
> This feature would be very easy to implement by checking the {{closure.parameterTypes.length}}. I know that 'cuz I have it implemented in my lib. I can provide a patch if this will help the feature to be implemented sooner.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)