Nir
08/29/2020, 8:09 PM[
("carrot", ["orange"]),
("banana", ["green", "yellow", "black"]),
("lemon", ["green", "yellow"]),
("apple", ["red", "green"]),
]
to
{
'orange': ['carrot'],
'green': ['banana', 'lemon', 'apple'],
'yellow': ['banana', 'lemon'],
'black': ['banana'],
'red': ['apple']
}
I was able to get it but it's pretty ugly. I tried to think whether there's anything substantially nicer in kotlin, and I couldn't think of anything. Curious to see what folks come up with 🙂Hullaballoonatic
08/29/2020, 8:18 PMelements.toMap()
Shawn
08/29/2020, 8:20 PMHullaballoonatic
08/29/2020, 8:21 PMHullaballoonatic
08/29/2020, 8:21 PMNir
08/29/2020, 8:22 PMNir
08/29/2020, 8:22 PMHullaballoonatic
08/29/2020, 8:26 PMelements.map { it.second }
.flatten()
.associateBy { k -> elements.filter { k in it.second }
.map { it.first } }
that's my first go 😮Hullaballoonatic
08/29/2020, 8:27 PMNir
08/29/2020, 8:30 PMHullaballoonatic
08/29/2020, 8:31 PMMark Spindler
08/29/2020, 11:18 PMHullaballoonatic
08/29/2020, 11:19 PMassociate
instead of just toMap
Nir
08/29/2020, 11:23 PMHullaballoonatic
08/29/2020, 11:23 PMNir
08/29/2020, 11:23 PMNir
08/29/2020, 11:24 PMNir
08/29/2020, 11:24 PMNir
08/29/2020, 11:24 PMNir
08/29/2020, 11:24 PMHullaballoonatic
08/29/2020, 11:25 PMmap
:`select`
or the c#/python naming for Map
:`Dictionary`Nir
08/29/2020, 11:25 PMHullaballoonatic
08/29/2020, 11:25 PMHullaballoonatic
08/29/2020, 11:25 PMMapping
Nir
08/29/2020, 11:27 PMMark Spindler
08/29/2020, 11:44 PMSelect
" is what Mathematica calls filter
, but Association
is basically mapOf
(and then AssociationMap
is associateWith
).Hullaballoonatic
08/29/2020, 11:48 PMfilter
select
, but then you have c# calling filter
where
and map
select
and suddenly going between languages gets really annoyingRuckus
08/30/2020, 12:47 AMThere are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
Pit
08/30/2020, 8:57 AMelements
.flatMap { (fruit, colours) -> colours.map { fruit to it } }
.groupBy(Pair<*, String>::second, Pair<String, *>::first)
the types are a bit ugly though