I agree with those people who are asking for more information about your use case. Without knowing more about the data you are trying to transform, it is difficult to recommend a concise solution that addresses your needs.
Please consider what I have to say in a provisional light, as I do not know enough about what you are trying to do.
You say that the tuple you are outputting can vary. Have you considered converting everything from Tuple to Vector?
So instead of
res17: Vector[(String, String)] = Vector((u, v),(w, x))
use
res17: Vector[Vector[String]] = Vector(Vector(u, v),Vector(w, x))
You can then easily transform from the Vector of Vector to a single Vector with a call to flatMap or flatten, which based on your question, it sounds like you already know how to do.
Another impression I get from reading your question is that if you want to keep a fixed length argument list that you transform from that argument list to a Vector, you might want to look at case classes instead of Tuple.
So instead of Tuple2 or Tuple3, define an inheritance hierarchy that allows the compiler to type check your program.
Something like this:
trait MyData
case class 2MemberData(data1: String, data2: String) extends MyData
case class 3MemberData(data1: String, data2: String, data3: String) extends MyData
case class 4MemberData(data1: String, data2: String, data3: String, data4: String) extends My Data
That way your function can output a value of type Vector[MyData] that you then flatten using pattern matching. So something like
def processData: Vector[MyData]
def cleanUp(input: Vector[MyData]): Vector[String]
where cleanUp is implemented as follows:
def cleanUp(input: Vector[MyData]): Vector[String] = {
input.flatMap{ d =>
d match {
case 2MemberData(data1, data2) => Vector(data1, data2)
case 3MemberData(data1, data2, data3) => Vector(data1, data2, data3)
case 4MemberData(data1, data2, data3, data4) => Vector(data1, data2, data3, data4)
}
}
}
I'm just throwing ideas out there and don't know if what I am saying is helpful or not. It really depends on what the surrounding code looks like. If you have any questions, feel free to ask.
.flatMap{case (a,b) => Vector(a,b)}. Add/adjust thecaseas needed for required tuple lengths.