I have an Array which is a result of a split function. Now I want all the elements of this array to be the elements of a tuple.
The number of elements are not more than 22.
I have an Array which is a result of a split function. Now I want all the elements of this array to be the elements of a tuple.
The number of elements are not more than 22.
Unfortunately you can't do that in a type-safe manner. Tuple arity must be known in the compile time. Your array's length is known only in runtime since collections have arbitrary lengths.
You probably want to do something with this tuples afterwards. Problem is you need to write a code that handles all tuple cases such as;
array match {
case Array(first)=> ???
case Array(first, second) => ???
case Array(first, second, third) => ???
...
case Array(first, second, third, fourth, fifth, sixth, seventh, .... twentysecond) => ???
case _ => // What to do now?
}
It is like this because we don't know what could be the outcome of this array -> tuple operation in compile-time, so we cover all the cases.
If we know how many elements our array has in the compile time we can use Shapeless to do something like this below, like answered in another question
import shapeless._
import HList._
import syntax.std.traversable._
val x = List(1, 2, 3)
val y = x.toHList[Int::Int::Int::HNil]
val z = y.get.tupled