Shawn
08/30/2019, 11:41 PMassociate { }
function that I’m missing? I’m trying to build a map and have multiple keys associated with the objects I’m iterating over - currently I’ve got something like this:
entries.flatMap { entry ->
(entry.aliases + entry.name).map { it to entry }
}.toMap()
which probably isn’t the most efficient approach 😬ushort
08/31/2019, 3:56 AMentries.associateBy( {it.name to it.aliases}, {it} )
karelpeeters
08/31/2019, 8:22 AMentry.name
and the keys entry.aliases
all to the same value entry
.ushort
09/02/2019, 6:02 PMentries.associateBy { it.keys }
If you meant the latter then I guess,
entries.fold(emptyList<String>()) { sum, element ->
sum + element.keys
}.associateWith { key -> entries.find { entry -> entry.keys.contains(key) } }
also
data class Entry(val name: String, val aliases: List<String>) {
val keys = (listOf(name) + aliases).toSet()
}
karelpeeters
09/02/2019, 10:55 PMfold { find { contains } }
!