I have List
in scala code which e.g i can receive from the specific method, and length of this list can vary from time to time, my target is converting this List
to Tuple
with same count of element:
val lst = someComputationHere()
val tupleLst = lstToTuple(lst)
clarification
//if lst.length = 2 --> Tuple2
//if lst.length = 3 --> Tuple3
//if lst.length = 4 --> Tuple4
...............
//if lst.length = N --> TupleN
and so on
p.s. I prefer don't use the reflection for solving this problem
UPD
My target is pass list with properties described above, to Slick
map clause
. Let's consider the next code snapshot:
query.map{ p=> (p.value1, p.value2, ... , p.valueN)}
Amount of this value is dinamic and i introduced this values
to list:
val lst = someComputationHere()
val tuples = toTuples(lst)//Problem is here
query.map{_ => tuples}
Problem description: query.map{}
- doen't pass the list, and as consequance, i must convert lst
with changeable length N to Tuple
with N elements
I don't know Slick, but if it only handles tuples it seems like a bad design choice for me. Even in Scala 2.12 there are no predefined types for tuples of arity greater than 22, although Scala will create them synthetically. So your best option is one big pattern match including all arities you need to support:
def toTuple[A](lst: List[A]): Product = lst match {
case _1 :: Nil => Tuple1(_1)
case _1 :: _2 :: Nil => Tuple2(_1, _2)
case _1 :: _2 :: _3 :: Nil => Tuple3(_1, _2, _3)
...
}