1

Is it possible to convert a vector of heterogeneous vectors to list of Tuple3 in Scala

i.e.

Vector(Vector(1,"a","b"),Vector(2,"b","c")) to List(Tuple3(1,"a","b"),Tuple3(2,"b","c"))
user2780187
  • 677
  • 7
  • 16

1 Answers1

8

Explicitly convert every inner Vector to Tuple3:

vector.map {
    case Vector(f, s, t) => Tuple3(f, s, t)
}.toList

If you have vectors of variadic size you 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 it has restriction: max length of vectors is 22.

Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • Can explicitly map it as the database driver returns a vector of vectors of variadic size i.e. it depends on the type of query executed ... if its select a,b from table, its a vector of size 2 representing the 2 columns chosen and so on ... Is there a way to map without taking the size into account – user2780187 Nov 27 '14 at 06:38
  • How do you expect to make `Tuple3`s if the vector lengths aren't 3? – Chris Martin Nov 27 '14 at 06:39
  • @ChrisMartin question was about `Tuple3`. I agree with you that vectors of variadic size need more general solution. – Sergii Lagutin Nov 27 '14 at 06:42
  • @SergeyLagutin I was replying to user2780187 (sorry that wasn't clear). – Chris Martin Nov 27 '14 at 06:43
  • The whole point is AnyRef here :) as the questions begs for a solution to deal with heterogeneous vectors ... As database rows contains variadic types – user2780187 Nov 27 '14 at 07:47
  • Answer accepted ... Albeit the type system has again managed to corner me as I was attempting to eventually take a Vector of heterogeneous vectors and create a list of Shapeless HLists . https://github.com/milessabin/shapeless/wiki/Migration-guide:-shapeless-1.2.4-to-2.0.0#tagged-types-and-newtype-have-moved ... Given the shortcoming, I attempted to convert the Vector to Tuples and then to HList. But import shapeless._; import syntax.std.product._ ; vector.map(toTuple).toList..map(_.productElements) fails with error: could not find implicit value for parameter gen: shapeless.Generic[Product] – user2780187 Nov 27 '14 at 08:29
  • @SergeyLagutin Would appreciate your advice if you are familiar with Shapeless – user2780187 Nov 27 '14 at 08:32
  • @user2780187 sorry, I'm not familiar with it. – Sergii Lagutin Nov 27 '14 at 08:34
  • I would upvote this answer if you hadn't added the second part! Better would be to say "don't do that". – Luigi Plinge Nov 27 '14 at 08:54
  • @LuigiPlinge I agree with you. Reflection is not scala way. – Sergii Lagutin Nov 27 '14 at 08:55
  • @user2780187 re: Shapeless my recommendation would be not to use it unless you really have to (and you don't mind your workmates hating you for the extra complexity). Find a way to work with the type system as it is - there is usually a way – Luigi Plinge Nov 27 '14 at 08:58