Is there an extended version of unzip in scala that works for any List [n-tuple] instead of just List [pairs], like Unzip?

If I have a list of 3 tuples, I want three separate lists. Is there a better way than this:

(listA, listB, listC) = (list.map(_._1), list.map(_._2). list.map(_._3))

which can work for any n-tuple?

EDIT: Although there are three unzip3s that I did not know about when writing this question, is there a way to write a function to get a total of n lists?

+4
source share
1 answer

How about this?

scala> val array = Array((1, 2, 3), (4, 5, 6), (7, 8, 9))
array: Array[(Int, Int, Int)] = Array((1,2,3), (4,5,6), (7,8,9))

scala> val tripleArray = array.unzip3
tripleArray: (Array[Int], Array[Int], Array[Int]) = (Array(1, 4, 7),Array(2, 5,8),Array(3, 6, 9))
0
source

All Articles