You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Paul King <pa...@asert.com.au> on 2022/12/19 11:23:19 UTC
Possible additional DGM collectEntries variants
Hi folks,
@mrhaki who many of you may know as the author of "Groovy Goodness" also
covers other topics and recently covered Kotlin's associate method:
https://blog.jdriven.com/2022/12/kotlin-kandy-transform-items-in-a-collection-to-a-map-with-associate/
Groovy addresses this use case fairly well using collectEntries, e.g.:
var languages = ['Kotlin', 'Groovy', 'Java', 'Clojure']
assert languages.collectEntries{ [it.toLowerCase(), it.size()] } ==
[kotlin:6, groovy:6, java:4, clojure:7]
assert languages.collectEntries{ [it.toLowerCase(), it] } ==
[kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
assert languages.collectEntries(Scala:5){ [it, it.size()] } ==
[Scala:5, Kotlin:6, Groovy:6, Java:4, Clojure:7]
But we don't have exact equivalents to associateWith and associateBy. The
collectEntries variants handle all the cases but there is some simplicity
that would come with additional equivalent variants, e.g.:
// "collectEntriesWith" could be just "collectEntries" if we want
assert languages.collectEntriesWith(String::toLowerCase, String::size) ==
[kotlin:6, groovy:6, java:4, clojure:7]
// equivalent of associateBy
assert languages.collectEntriesWithKey(String::toLowerCase) ==
[kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
// equivalent of associateWith
assert languages.collectEntriesWithValue(String::size) ==
[Kotlin:6, Groovy:6, Java:4, Clojure:7]
The method names are just suggestions at this point.
Thoughts?
Cheers, Paul.
Re: Possible additional DGM collectEntries variants
Posted by Paul King <pa...@asert.com.au>.
I created https://issues.apache.org/jira/browse/GROOVY-10879 to track this
issue. It points to the current proposed PR.
Currently I am proposing a few additional overloads and a few additional
specific methods. I think this gives a good balance between improved typing
information and succinct representation of the various cases.
Cheers, Paul.
On Tue, Dec 20, 2022 at 6:20 PM Mario Garcia <ma...@gmail.com> wrote:
> +1.
>
> Hi all. For me it makes more sense to have a specific method for that
> (collectEntriesWith, collectEntriesWithKey...) than overloading
> collectEntries and forcing me to introduce the java.util.function api just
> for so little.
>
> El lun, 19 dic 2022 a las 12:23, Paul King (<pa...@asert.com.au>)
> escribió:
>
>>
>> Hi folks,
>>
>> @mrhaki who many of you may know as the author of "Groovy Goodness" also
>> covers other topics and recently covered Kotlin's associate method:
>>
>>
>> https://blog.jdriven.com/2022/12/kotlin-kandy-transform-items-in-a-collection-to-a-map-with-associate/
>>
>> Groovy addresses this use case fairly well using collectEntries, e.g.:
>>
>> var languages = ['Kotlin', 'Groovy', 'Java', 'Clojure']
>> assert languages.collectEntries{ [it.toLowerCase(), it.size()] } ==
>> [kotlin:6, groovy:6, java:4, clojure:7]
>> assert languages.collectEntries{ [it.toLowerCase(), it] } ==
>> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
>> assert languages.collectEntries(Scala:5){ [it, it.size()] } ==
>> [Scala:5, Kotlin:6, Groovy:6, Java:4, Clojure:7]
>>
>> But we don't have exact equivalents to associateWith and associateBy. The
>> collectEntries variants handle all the cases but there is some simplicity
>> that would come with additional equivalent variants, e.g.:
>>
>> // "collectEntriesWith" could be just "collectEntries" if we want
>> assert languages.collectEntriesWith(String::toLowerCase, String::size) ==
>> [kotlin:6, groovy:6, java:4, clojure:7]
>> // equivalent of associateBy
>> assert languages.collectEntriesWithKey(String::toLowerCase) ==
>> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
>> // equivalent of associateWith
>> assert languages.collectEntriesWithValue(String::size) ==
>> [Kotlin:6, Groovy:6, Java:4, Clojure:7]
>>
>> The method names are just suggestions at this point.
>>
>> Thoughts?
>>
>> Cheers, Paul.
>>
>>
>>
>>
Re: Possible additional DGM collectEntries variants
Posted by Mario Garcia <ma...@gmail.com>.
+1.
Hi all. For me it makes more sense to have a specific method for that
(collectEntriesWith, collectEntriesWithKey...) than overloading
collectEntries and forcing me to introduce the java.util.function api just
for so little.
El lun, 19 dic 2022 a las 12:23, Paul King (<pa...@asert.com.au>) escribió:
>
> Hi folks,
>
> @mrhaki who many of you may know as the author of "Groovy Goodness" also
> covers other topics and recently covered Kotlin's associate method:
>
>
> https://blog.jdriven.com/2022/12/kotlin-kandy-transform-items-in-a-collection-to-a-map-with-associate/
>
> Groovy addresses this use case fairly well using collectEntries, e.g.:
>
> var languages = ['Kotlin', 'Groovy', 'Java', 'Clojure']
> assert languages.collectEntries{ [it.toLowerCase(), it.size()] } ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> assert languages.collectEntries{ [it.toLowerCase(), it] } ==
> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
> assert languages.collectEntries(Scala:5){ [it, it.size()] } ==
> [Scala:5, Kotlin:6, Groovy:6, Java:4, Clojure:7]
>
> But we don't have exact equivalents to associateWith and associateBy. The
> collectEntries variants handle all the cases but there is some simplicity
> that would come with additional equivalent variants, e.g.:
>
> // "collectEntriesWith" could be just "collectEntries" if we want
> assert languages.collectEntriesWith(String::toLowerCase, String::size) ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> // equivalent of associateBy
> assert languages.collectEntriesWithKey(String::toLowerCase) ==
> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
> // equivalent of associateWith
> assert languages.collectEntriesWithValue(String::size) ==
> [Kotlin:6, Groovy:6, Java:4, Clojure:7]
>
> The method names are just suggestions at this point.
>
> Thoughts?
>
> Cheers, Paul.
>
>
>
>
RE: [EXT] Possible additional DGM collectEntries variants
Posted by "Milles, Eric (TR Technology) via dev" <de...@groovy.apache.org>.
True, no direct equivalents for associateBy and associateWithValue. But "collectEntries{ [it.toLowerCase(), it] }" is pretty succinct.
If you wanted to extend with just one new extension of collectEntries, I think ".collectEntries(keyFun, Function.identity())" and ".collectEntries(Function.identity(), valFun)" is the associateBy and associateWithValue. Java streams gives you a lot of options here. Guava provides "uniqueIndex" to do the same, but provides stronger checks on key uniqueness. https://guava.dev/releases/23.0/api/docs/com/google/common/collect/Maps.html#uniqueIndex-java.lang.Iterable-com.google.common.base.Function-
Re: Possible additional DGM collectEntries variants
Posted by Daniel Sun <su...@apache.org>.
+1
Cheers,
Daniel Sun
On 2022/12/19 11:23:19 Paul King wrote:
> Hi folks,
>
> @mrhaki who many of you may know as the author of "Groovy Goodness" also
> covers other topics and recently covered Kotlin's associate method:
>
> https://blog.jdriven.com/2022/12/kotlin-kandy-transform-items-in-a-collection-to-a-map-with-associate/
>
> Groovy addresses this use case fairly well using collectEntries, e.g.:
>
> var languages = ['Kotlin', 'Groovy', 'Java', 'Clojure']
> assert languages.collectEntries{ [it.toLowerCase(), it.size()] } ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> assert languages.collectEntries{ [it.toLowerCase(), it] } ==
> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
> assert languages.collectEntries(Scala:5){ [it, it.size()] } ==
> [Scala:5, Kotlin:6, Groovy:6, Java:4, Clojure:7]
>
> But we don't have exact equivalents to associateWith and associateBy. The
> collectEntries variants handle all the cases but there is some simplicity
> that would come with additional equivalent variants, e.g.:
>
> // "collectEntriesWith" could be just "collectEntries" if we want
> assert languages.collectEntriesWith(String::toLowerCase, String::size) ==
> [kotlin:6, groovy:6, java:4, clojure:7]
> // equivalent of associateBy
> assert languages.collectEntriesWithKey(String::toLowerCase) ==
> [kotlin:'Kotlin', groovy:'Groovy', java:'Java', clojure:'Clojure']
> // equivalent of associateWith
> assert languages.collectEntriesWithValue(String::size) ==
> [Kotlin:6, Groovy:6, Java:4, Clojure:7]
>
> The method names are just suggestions at this point.
>
> Thoughts?
>
> Cheers, Paul.
>