Is it possible to zip 3 lists? I am zipping 2 here...
# getting-started
n
Is it possible to zip 3 lists? I am zipping 2 here, but I would like to throw a list of icons into the mix. How would it work in Kotlin?
Copy code
@Composable
fun ShowDropDown(
    openDropDown: MutableState<Boolean>,
    text: List<String>,
    destination: List<DirectionDestination>,
    navigator: DestinationsNavigator,
) {
    DropdownMenu(
        expanded = openDropDown.value,
        onDismissRequest = { openDropDown.value = false },
    ) {
        text.zip(destination)
            .forEach {
                DropdownMenuItem(onClick = { navigator.navigate(it.second) },
                text = { Text(it.first) })
            }

    }
}
l
What do you want in the zipped list? You could do
list1.zip(list2.zip(list3))
, and it would be a List<Pair<A, Pair<B, C>>>, where A, B, and C are the types of list1, list2, and list3.
If you wanted a Triple, you could add
Copy code
.map { abc ->
   Triple(abc.first, abc.second.first, abc.second.second)
}
n
oh great, thanks for this!
l
I would write an extension for this to keep everything clean. When you nest Pairs, it gets hard to read.
👍 1
m
late to the party, but you could use the transform part of the zip function to keep it nice and readable along the way
Copy code
list1.zip(list2).zip(list3) { (a, b), c -> Triple(a, b, c) }
❤️ 5
829 Views