Is there way to create tuple from list(without codegeneration)?

后端 未结 6 2196
[愿得一人]
[愿得一人] 2020-11-27 17:44

Sometimes there are needs to create tuples from small collections(for example scalding framework).

def toTuple(list:List[Any]):scala.Product = ...

6条回答
  •  执笔经年
    2020-11-27 18:11

    If, as @dhg observed, you know the expected arity up front you can do something useful here. Using shapeless you could write,

    scala> import shapeless._
    import shapeless._
    
    scala> import Traversables._
    import Traversables._
    
    scala> import Tuples._
    import Tuples._
    
    scala> List(1, 2, 3).toHList[Int :: Int :: Int :: HNil] map tupled
    res0: Option[(Int, Int, Int)] = Some((1,2,3))
    

提交回复
热议问题