Zip multiple sequences

后端 未结 7 1738
眼角桃花
眼角桃花 2020-12-06 09:20

I am trying to zip multiple sequences to form a long tuple:

val ints = List(1,2,3)
val chars = List(\'a\', \'b\', \'c\')
val strings = List(\"Al         


        
7条回答
  •  囚心锁ツ
    2020-12-06 09:35

    Using shapeless, you could do:

    import shapeless.Tuples._
    
    val ints = (1, 2, 3)
    val chars = ('a', 'b', 'c')
    
    val megatuple = (ints, chars)
    
    val megahlist = (megatuple hlisted) map hlisted
    
    val transposed = (mhlist transpose) map tupled tupled
    
    scala> transposed
    res: ((Int, Char), (Int, Char), (Int, Char)) = ((1,a),(2,b),(3,c))
    

    (not sure, if there are more implicts defined which lets you avoid the map and back-and-forth conversions)

    [Edit: This part is not true anymore.

    Note that the shapeless docs say, only conversions up to Tuple4 are currently supported. You’d have to manually create the HLists then.]

提交回复
热议问题