简体   繁体   English

如何“对抗”akka-streams Sink

[英]How to “contramap” akka-streams Sink

Having an akka-streams Sink : akka-streams Sink

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

and a function from Bar to Foo : 以及从BarFoo的功能:

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. 我想contramap( 地图的对面) sinkf得到类型的水槽Sink[Bar, Any] ,但无法找到在库中这样的简单方法。 How to achieve what I need? 如何实现我的需求?

It turned out to be rather simple. 事实证明这很简单。

Create a Flow accepting Bar s: 创建Flow接受Bar

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

and map it with f pipelining results to the original sink : 并使用f pipelining结果将其映射到原始接收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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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