Daniele B
08/08/2020, 9:55 PMmylist.sortedBy{it.quantity}.map {
NewElement(
position = (how to retrieve the index of the element in the sorted list?)
name = it.name,
quantity = it.quantity
)
}
is there any way to retrieve the index of the element?nanodeath
08/08/2020, 9:57 PM.mapIndexed
.asSequence()
and end with .toList()
to avoid allocating more lists than you need.Daniele B
08/08/2020, 10:01 PMlist.asSequence().sortedBy{it.quantity}.toList().mapIndexed { index, elem ->
NewElement(
position = index
name = elem.name,
quantity = elem.quantity
)
}
nanodeath
08/08/2020, 10:08 PMtoList()
should usually be at the end, otherwise you got itDaniele B
08/08/2020, 10:08 PMasSequence()
and then toList()
?nanodeath
08/08/2020, 10:09 PMfoo.map {}.map {}.reduce{}
or whatever, every step generates a new list. if you say asSequence().map {}.map{}.map{}.reduce{}.toList()
(or toSet()
), it minimizes the use of intermediate collections by doing fancy lazy iterator stuffsortedBy
almost certainly creates a list internally, but...things like map
don't)Daniele B
08/08/2020, 10:11 PMnanodeath
08/08/2020, 10:11 PM