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.
