简体   繁体   中英

How to “contramap” akka-streams Sink

Having an akka-streams Sink :

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

and a function from Bar to Foo :

val f: Bar => Foo = ???

I want to contramap (the opposite of map ) sink with f to get a sink of type Sink[Bar, Any] , but can't find such a simple method in the library. How to achieve what I need?

It turned out to be rather simple.

Create a Flow accepting Bar s:

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

and map it with f pipelining results to the original sink :

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

使用akka-streams版本2.4.X它甚至更简单:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM