How to destructure Kotlin nested pairs with forEach

539 views Asked by At

I need to destructure Kotlin nested pairs. How can I do this simply without using pair.first/pair.second?

val chars = listOf('A', 'B', 'C')
val ints = listOf(1, 2, 3)
val booleans = listOf(true, false, false)

val cib: List<Pair<Pair<Char, Int>, Boolean>> = chars.zip(ints).zip(booleans)
cib.forEach { ((c, i), b) -> // compile error
    println("$c $i $b")
}
1

There are 1 answers

0
AlexT On

Not sure if there really is a way of desctructuring a Pair<Pair<*,*>> straight away, but you could do this:

cib.forEach { (pair, b) -> 
    val (c, i) = pair
    //do stuff with c, i, b
}