https://kotlinlang.org logo
Title
n

Nick Halase

07/06/2022, 6:43 PM
Is there a more elegant way to do this with the Kotlin standard lib?
val values = listOf("name", "ASC", "description", "DESC")
val propNames = values.filterIndexed { index, _ -> index.mod(2) == 0 }
val sortDirections = values.filterIndexed { index, _ -> index.mod(2) != 0 }
propNames zip sortDirections // [(name, ASC), (description, DESC)]
e

ephemient

07/06/2022, 6:48 PM
values.chunked(2) { (k, v) -> k to v }
or
.windowed(2, 2)
if you want to ignore odd-length inputs instead of crashing
2
:nice: 4
n

Nick Halase

07/06/2022, 6:50 PM
oh nice! I forgot about chunked, but I didn't know windowed existed.