简体   繁体   English

使用 akka-stream 过滤异步

[英]Filter async with akka-stream

Is there an equivalent to the mapAsync() method, but for filter ?是否有等效于mapAsync()方法,但对于filter

Here is an example using pseudo code:下面是一个使用伪代码的例子:

val filter: T => Future[Boolean] = /.../

source.filter(filter).runWith(/.../)
       ^^^^^^

Thanks谢谢

I don't think there is a direct method of Flow or Source that has the capability you're looking for, but a combination of the available methods will get you what you want:我认为没有一种直接的FlowSource方法具有您正在寻找的功能,但是可用方法的组合将为您提供您想要的:

def asyncFilter[T](filter: T => Future[Boolean], parallelism : Int = 1)
                  (implicit ec : ExecutionContext) : Flow[T, T, _] =
  Flow[T].mapAsync(parallelism)(t => filter(t).map(_ -> t))
         .filter(_._1)
         .map(_._2)

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

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