Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destructure Kotlin nested pairs with forEach

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")
}
like image 816
AndroidDev Avatar asked Oct 27 '25 08:10

AndroidDev


1 Answers

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
}
like image 134
AlexT Avatar answered Oct 29 '25 09:10

AlexT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!