Is there a `List<E>.associateWithIndex(): Ma...
# stdlib
s
Is there a
List<E>.associateWithIndex(): Map<E, Int>
or something similar that I am missing. (edited)
m
You have
mapWithIndex
,
filterWithIndex
,
forEachWithIndex
and maybe some more to do some processing.
If you need index with and item as an end result, I'd go with
mapWithIndex
that will map item + index in some model (or Pair if that's good enough)
s
I ended up doing
.indexed().associateBy({ it.value }) { it.index }
. I just kind of expected it to be there but wasnt sure about the name
a
how about
.mapIndexed(::Pair).toMap()
although now I see you want a map of values to indices so that would have to be
.mapIndexed { i, v -> v to i }.toMap()