Here's a fun list/map etc challenge i came across ...
# announcements
n
Here's a fun list/map etc challenge i came across in a question in the python slack. Someone was asking how to do this in a dict comprehension, instead of with looping in python:
Copy code
[
        ("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 🙂
h
There's no tuple type in kotlin, so assuming those are comping in as a list of pairs, this is as simple as:
Copy code
elements.toMap()
s
that’s not the transform at all though
h
oh, i guess i didn't read the contents, ha
yeah, i should read <_<
n
Just assume the first thing is a list of pairs of string, list of string
Second is a map from string to list of string
h
Copy code
elements.map { it.second }
        .flatten()
        .associateBy { k -> elements.filter { k in it.second }
                                    .map { it.first } }
that's my first go 😮
i think there's probably a more efficient way to do it with grouping or something
n
I was thinking about it. It's not easy though. The classic for loop and mutation looks pretty good here
h
yeah, i think the best way to do it and by-far the easiest to read is a for-loop
m
Casey, I think you might have meant "associateWith" for the map to go in the right direction. But other than that I came up with an identical approach.
👍 1
h
It's annoying that JB called these methods
associate
instead of just
toMap
n
Probably worried that it was too similar to map
h
yep, overloaded word!
n
The real tragedy is that the data structures produced by map and associate are "basically" immutable
Since they are the only reference to the produced structure, and they are read only
Yet it's not reflected in the type system
map should return an ImmutableList
Which is an interface that refines List
h
could have done the sql/c# naming for
map
:`select` or the c#/python naming for
Map
:`Dictionary`
n
I do like dictionary
h
same
Or maybe just
Mapping
n
Mapping is what python calls the interfaces
m
Confusingly (when I switch between things), "
Select
" is what Mathematica calls
filter
, but
Association
is basically
mapOf
(and then
AssociationMap
is
associateWith
).
h
yeah i understand calling
filter
select
, but then you have c# calling
filter
where
and
map
select
and suddenly going between languages gets really annoying
🤯 1
r
There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
p
Copy code
elements
        .flatMap { (fruit, colours) -> colours.map { fruit to it } }
        .groupBy(Pair<*, String>::second, Pair<String, *>::first)
the types are a bit ugly though