I’d love to have `fun <T> Iterable<Iterab...
# advent-of-code
j
I’d love to have
fun <T> Iterable<Iterable<T>>.transpose()
in stdlib 🙂
🙌 4
k
I have one, but it is on lists cause it requires the lengths to match. I did use it today =)
f
I was sure I've written one for a previous AOC but couldn't find it - so ended up using an
Int
-indexed map instead.
n
I did roll my own, but I agree, it would be very helpful in stdlib 😊
Copy code
private fun <T> transpose(list: List<List<T>>): List<List<T>> {
        val n = list.maxOfOrNull { it.size }!!

        val iterList = list.map { it.iterator() }

        return (0 until n)
            .map {
                iterList
                    .filter { it.hasNext() }
                    .map { it.next() }
            }
    }