Elidor00
Elidor00

Reputation: 1622

Is it possible to flatten this particular sequence in scala?

I have this structure: Seq[(Int, mutable.Set[List[A]])] and my aim is flatten it in order to obtain a structure like List[List[A]].

In particular, my structure is something like this: val order = List((1,HashSet(List('o'))), (4,HashSet(List('i', 'j', 'k', 'l'))), (3,HashSet(List('a', 'b', 'c'), List('f', 'g', 'h'))), (2,HashSet(List('d', 'e'), List('m', 'n'), List('z', 'x')))) and i want to obtain something like val order = List( List('o'), List('i', 'j', 'k', 'l'), List('a', 'b', 'c'), ...).

I tried to make a map in this way:

order map {
      case (_, Set[List[A]]) => List[A]
}

but it doesn't work. The error is "pattern type is incompatible with expected type". The same thing also happens with: case (Seq[(_, Set[List[A]])]) => List[A] and case (Seq[(_, mutable.Set[List[A]])]) => List[A].

I'm pretty sure that to do the flattening, the solution is to use the map (or flatMap), but apparently I'm using it in the wrong way. Anyone have any suggestions / ideas of how I could do it? Thanks!

Upvotes: 0

Views: 237

Answers (1)

Tim
Tim

Reputation: 27356

How about this?

order.flatMap(_._2)

Upvotes: 3

Related Questions