Convert Vector to Tuple scala -
is possible convert vector of heterogeneous vectors list of tuple3 in scala
i.e.
vector(vector(1,"a","b"),vector(2,"b","c")) list(tuple3(1,"a","b"),tuple3(2,"b","c"))
explicitly convert every inner vector
tuple3
:
vector.map { case vector(f, s, t) => tuple3(f, s, t) }.tolist
if have vectors of variadic size can use more general approach:
def totuple(seq: seq[_]): product = { val clz = class.forname("scala.tuple" + seq.size) clz.getconstructors()(0).newinstance(seq.map(_.asinstanceof[anyref]): _*).asinstanceof[product] } vector.map(totuple).tolist
but has restriction: max length of vectors 22.
Comments
Post a Comment