【问题标题】:Transform Enumerator[T] into List[T]将 Enumerator[T] 转换为 List[T]
【发布时间】:2014-10-04 18:08:09
【问题描述】:

我正在将使用 ReactiveMongo 的应用程序与旧版应用程序集成。

因为,我必须在某些时候维护遗留应用程序接口,我必须阻止和/或将我的代码转换为指定的接口类型。我将该代码简化为下面的示例。

有没有比 getChunks 更好的方法来消耗所有输出类型为 List 的 Enumerator?标准做法是什么?

implicit def legacyAdapter[TInput,TResult]
  (block: Future[Enumerator[TInput]])
  (implicit translator : (TInput => TResult), 
            executionContext:ExecutionContext,
            timeOut : Duration): List[TResult] = {

  val iter = Iteratee.getChunks[TResult]
  val exhaustFuture = block.flatMap{
    enumy => { enumy.map(i => translator(i) ).run(iter) }
  }

  val r  = Await.result(exhaustFuture , timeOut)
  r
}

【问题讨论】:

    标签: scala playframework-2.0 reactivemongo


    【解决方案1】:

    Iteratee.getChunks 是 playframework 提供的唯一实用程序,它通过消耗枚举数的所有块来构建列表,您当然可以使用 Iteratee.fold 做同样的事情,但您将重新发明轮子,因为 Iteratee.getChunks 使用 Iteratee。折叠。

    【讨论】:

      【解决方案2】:

      我们发现公开 http://reactivemongo.org/releases/0.10/api/index.html#reactivemongo.api.Cursor 的 Collect 方法比收集 Enumerator 的块更高效。所以从某种意义上说,我们通过改变问题来解决我们的问题。

      这确实意味着我们数据层的 API 发生了变化,但性能改进允许这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        相关资源
        最近更新 更多