Like "contramap" akka-streams Sink

The presence of akka streams Sink:

val sink: Sink[Foo, Any] = ???

and function from Barto Foo:

val f: Bar => Foo = ???

I want to contramap (contrast card) Sinkwith f, to get the receiver type Sink[Bar, Any], but can not find a simple method in the library. How to achieve what I need?

+4
source share
2 answers

It turned out to be pretty simple.

Create Flowaccepting Bars:

val flow: Flow[Bar, Bar, Unit] = Flow[Bar]

and compare it with the results of <pipelining fto the original sink:

val sink2: Sink[Bar, Unit] = flow.map(f).to(sink)
+2
source

With akka-streamsversion, 2.4.Xthis is even simpler:

val sink3: Sink[Bar, Future[Done]] = sink.contramap(f)
+2
source

Source: https://habr.com/ru/post/1619963/


All Articles