Anthony
Anthony

Reputation: 35998

How to execute an Akka Streams Source?

I have a Source like this:

val colorSource = myMethod(ctx, id)
  .via(framing("\n"))
  .map(_.utf8String)
  .map(_.trim)
  .map(c => ColorParser(c))
  .collect {
    case Right(color) => color
  }

How can I actually run this and get List[Color]? For starters, I would like to print it to view the contents, and then also be able to pass around the List[Color] to other methods.

I did this to print it, but it didn't work:

val values = for {
  t <- colorSource.runWith(Sink.ignore)
} yield t
total.map(println _)

Upvotes: 0

Views: 95

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

To obtain a Future[List[Color]]:

val colorList: Future[List[Color]] =
  colorSource
    .runWith(Sink.seq[Color])
    .map(_.toList)

If you want to just print the contents of the stream:

colorSource.runForeach(println)

Upvotes: 2

Related Questions