Endre Deak
12/16/2021, 4:56 PM// s is my input representation and once a portion is processed, I remove that portion
val sub = mutableListOf<Packet>()
while(!s.isEmpty()) {
sub.add(processPacket(s))
}
return sub
Endre Deak
12/16/2021, 4:56 PMfold
but with an exit conditionelizarov
12/16/2021, 5:22 PMprocessPacket
One you’ve transformed it into pure functions, you can start looking for nicer functional alternatives.elizarov
12/16/2021, 5:24 PMEndre Deak
12/16/2021, 5:25 PMArrayDeque
so I don’t have to trace the current position, I’m always at the headephemient
12/16/2021, 6:18 PMephemient
12/16/2021, 6:19 PMMichael Böiers
12/16/2021, 9:16 PMgenerateSequence { if (s.isEmpty()) null else processPacket(s) }.toList()
nkiesel
12/17/2021, 2:05 AMreturn buildList { while (s.isNotEmpty()) add(processPacket(s)) }
Michael Böiers
12/17/2021, 8:44 PMreturn mutableListOf<...>().apply { while (s.isNotEmpty()) add(processPacket(s)) }
if you still need it to be a mutable list 🙂Endre Deak
12/17/2021, 10:25 PM