How to flatten a Flow[T, Seq[Seq[String]], NotUsed] into a Flow[T, Seq[String], NotUsed]

Viewed 1038

I have a stream of the type Flow[T, Seq[Seq[String]], NotUsed].

I would like to flatten it in a way that the example stream

ev1: Seq(Seq("a", "b"), Seq("n", "m")
ev2: Seq(Seq("x", "y"))

should become the following stream:

ev1: Seq("a", "b")
ev2: Seq("n", "m")
ev3: Seq("x", "y")
1 Answers

Use mapConcat(identity):

  implicit val actorSystem = ActorSystem()
  implicit val materializer = ActorMaterializer()

  val events = Vector(
    Vector(Vector(1, 2), Vector(3, 4)),
    Vector(Vector(5, 6))
  )

  Source.apply(events)
    .mapConcat(identity)
    .runForeach(println)

  actorSystem.terminate()

prints

Vector(1, 2)
Vector(3, 4)
Vector(5, 6)

In general, with mapConcat you can flatten events-sequences to the main stream.

Related