2

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.

6
  • 1
    Note that your desiredData is slightly misleading. I assume you wanted Seq(Seq(1,2,3,4), Seq(5,6,7,8)). (What you currently have is of type: Seq[(Int, Int, Int, Int)] and not Seq[Seq[Int]]) Commented Jan 26, 2016 at 3:31
  • @gzm0 thanks for catching that. it is now fixed. Commented Jan 26, 2016 at 3:42
  • Hold on, what do you mean by "throwing me for a loop"? Is this actually looping or do you just not understand what ? 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). Commented Jan 26, 2016 at 3:54
  • @gzm0 sorry for the confusion. my choice of words wasn't great. what I mean was that I didn't understand the ? mean in this context. Commented Jan 26, 2016 at 4:04
  • Btw, the above code works. I am not sure what the question is here? Commented Jan 26, 2016 at 4:18

2 Answers 2

6

mapValues on Map is lazy (unlike any other method on a default Map). So that might be the issue there. Have you tried:

data.groupBy(_._1).map(_._2.map(_._3).toArray)

Note that the toArray is completely optional here.

Sign up to request clarification or add additional context in comments.

Comments

3
val result: List[Seq[Int]] =
  data.groupBy(_._1).mapValues(_.map(_._3)).values.toList

1 Comment

Can you explain why this one works and the OP's doesn't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.