Quizzie
Quizzie

Reputation: 879

Akka: Transforming the Input of a Sink with a Materialized Value

I have a sink: Sink[String, Mat] and want to transform it into the sink: Sink[Int, Mat] by mapping each element: num: Int => ("num" + num): String and keeping the original materialized type and value.

If Mat was Unit, then it's easy:

def transformSink(sink: Sink[String, Unit]): Sink[Int, Unit] =
  Flow[Int].map("num" + _).to(sink)

But what about transforming a sink for any Mat?

def transformSink[Mat](sink: Sink[String, Mat]): Sink[Int, Mat] = ???

Upvotes: 0

Views: 576

Answers (1)

cmbaxter
cmbaxter

Reputation: 35443

I think toMat is what you are looking for. See if this works for you:

def transformSink[Mat](sink: Sink[String, Mat]): Sink[Int, Mat] =
  Flow[Int].map("num" + _).toMat(sink)(Keep.right)
}

Upvotes: 2

Related Questions