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.mapIndexednanodeath
08/08/2020, 9:58 PM.asSequence() and end with .toList() to avoid allocating more lists than you need.Daniele B
08/08/2020, 10:01 PMDaniele B
08/08/2020, 10:07 PMDaniele B
08/08/2020, 10:07 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() ?Daniele B
08/08/2020, 10:09 PMnanodeath
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 stuffnanodeath
08/08/2020, 10:10 PMnanodeath
08/08/2020, 10:11 PMsortedBy 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 PMnanodeath
08/08/2020, 10:11 PM