I am trying to convert data that looks like this:
val inputData =
Seq(("STUDY1", "Follow-up", 1),
("STUDY1", "Off Study", 2),
("STUDY1", "Screening", 3),
("STUDY1", "Treatment", 4),
("STUDY2", "Follow-up", 5),
("STUDY2", "Off Study", 6),
("STUDY2", "Screening", 7),
("STUDY2", "Treatment", 8));
into data that looks like this:
val desiredData =
Seq(Seq(1,2,3,4),
Seq(5,6,7,8));
The closest that I've gotten is with this:
val result: Map[String, Seq[Int]] =
data.groupBy(i => i._1)
.mapValues(j => j.map(k => k._3))
.mapValues(_.toArray)
result.values.toSeq
This yields:
res0: Seq[Seq[Int]] = Stream(WrappedArray(1, 2, 3, 4), ?)
That last question mark is throwing me for a loop.
EDIT
Future internet travelers who land here: my code actually did work ... my confusion stemmed from understanding what the ? was all about. Answers from folks down below helped me see that mapValues did lazy evaluation, and that the ? simply implies that.
desiredDatais slightly misleading. I assume you wantedSeq(Seq(1,2,3,4), Seq(5,6,7,8)). (What you currently have is of type:Seq[(Int, Int, Int, Int)]and notSeq[Seq[Int]])?means in this context? I tried this and it doesn't loop (on 2.11.7). So?essentially means that the tail of the stream is lazily evaluated and not evaluated here (this is to prevent infinite loops in case of infinite streams).?mean in this context.