Is there a safe way in Scala to transpose a List of unequal-length Lists?

后端 未结 5 968
南方客
南方客 2020-12-03 11:37

Given the following List:

val l = List(List(1, 2, 3), List(4, 5), List(6, 7, 8))

If I try to transpose it, Scala will throw the following e

5条回答
  •  时光取名叫无心
    2020-12-03 12:20

    How about this one-liner using the Scala's standard Api:

    ((l map (_.toArray)) toArray).transpose map (_.toList) toList
    

    This gets the job done and is O(N*M), where N is the length of the wrapper list and M is the length of the longest list inside the wrapper list.

提交回复
热议问题