hi gang, is there any nice, easily understandable/...
# codereview
hi gang, is there any nice, easily understandable/identifiable way to turn a list of Byte (
) into a list of Shorts? (
) such that [ 0x10, 0x01 ] would become [ 0x1001 ]
Copy code
private fun List<Byte>.toShorts(): List<Short> {
    val result = mutableListOf<Short>()
    for (i in 0 .. size - 2 step 2) {
        result.add((this[i].toInt().shl(8) + this[i+1]).toShort())
    return result
Is what I've got, which is I guess understandable, but I wonder if there is a more kotlin'y way to iterate over pairs of list items to them map them into something else
Use windowed/chunked to split the list into pairs and then map them to shorts (but it is less efficient than your initial option, if you care about it)
you can simplify the loop in your function to
Copy code
for (i in indices step 2) {
or change indexing to something which also pre-sizes the output
Copy code
List(size / 2) { i ->
    (this[2 * i].toUInt() shl 8 or this[2 * i + 1].toUInt()).toShort()
cheers guys! I like the windowing approach, I didn't even consider that, but didn't realise i in indices was a thing either.