LastExceed
01/01/2020, 2:37 PMfun foo(lastEntry: Entry) {
val myList = mutableListOf<Entry>()
var currentEntry = lastEntry
do {
myList.add(currentEntry)
currentEntry = currentEntry.parent
} while (currentEntry != null)
myList.reverse()
}
how can i simplify this pseudo code so I don't have to perform .reverse()
at the end ?Kashif
01/01/2020, 2:39 PMLastExceed
01/01/2020, 2:40 PMKashif
01/01/2020, 2:41 PMKashif
01/01/2020, 2:41 PMLastExceed
01/01/2020, 2:42 PMLastExceed
01/01/2020, 2:43 PMKashif
01/01/2020, 2:57 PMLastExceed
01/01/2020, 2:58 PMKashif
01/01/2020, 3:00 PMLastExceed
01/01/2020, 3:04 PMfun foo(lastElement: Element) {
val myList = mutableListOf<Element>()
fun addAfterParent(element: Element) {
if (element.parent != null) addAfterParent(element.parent)
myList.add(element)
}
}
Dominaezzz
01/01/2020, 3:50 PMval list = generateSequence(lastEntry) { it.parent }.toMutableList().reverse()
Dominaezzz
01/01/2020, 3:51 PMLastExceed
01/01/2020, 3:52 PMyou're converting a linked listthats not necessarily the case
LastExceed
01/01/2020, 3:52 PMDominaezzz
01/01/2020, 3:54 PMcurrentEntry.parent
is of unknown size right?LastExceed
01/01/2020, 3:55 PMLastExceed
01/01/2020, 3:55 PMLastExceed
01/01/2020, 3:56 PMDominaezzz
01/01/2020, 3:57 PMDominaezzz
01/01/2020, 3:57 PMLastExceed
01/01/2020, 3:57 PMDominaezzz
01/01/2020, 3:59 PMAbstractList
wrapper that accesses the underlying List
with the reverse index. 😛Dominaezzz
01/01/2020, 4:01 PMclass ReverseList<T>(private val list: List<T>) : AbstractList<T>() {
override val size: Int get() = list.size
override fun get(index: Int): T = list.get(list.lastIndex - index)
}
LastExceed
01/01/2020, 4:02 PMDominaezzz
01/01/2020, 4:06 PMLastExceed
01/01/2020, 4:07 PMDominaezzz
01/01/2020, 4:09 PMMutableList
) and then iterates through in the reverse order.LastExceed
01/01/2020, 4:12 PMDominaezzz
01/01/2020, 4:16 PMreverse
function? Or are you trying to optimise?LastExceed
01/01/2020, 4:16 PMDominaezzz
01/01/2020, 4:24 PM