【问题标题】:How to get an Enumerator[T] from List[T] when using Play 2 Iteratees使用 Play 2 Iteratees 时如何从 List[T] 中获取 Enumerator[T]
【发布时间】:2013-02-13 16:49:17
【问题描述】:

我对 Play 2 很陌生,正在尝试 Iteratees。

第一季度。如何从List[Person] 获得Enumerator[Person]

第二季度。当我尝试将Enumerator(Option[String]) 传递给Ok.stream 时,我在控制台上收到一条错误消息,指出

无法将 Option[String] 的实例写入 HTTP 响应。尝试定义一个 Writeable[Option[String]]

有人能指出我如何为自定义类型定义可写的 HTTP 响应吗?

谢谢。

【问题讨论】:

    标签: functional-programming playframework-2.0 enumerator iterate


    【解决方案1】:

    A1。您可以使用Enumerator#enumerate 为每个Person 运行Iteratee

    val persons: List[Person] = List(person0, person1)  
    Enumerator.enumerate(persons) |>>> Iteratee.foreach(println _)
    

    https://github.com/playframework/Play20/blob/2.1.0/framework/src/iteratees/src/test/scala/play/api/libs/iteratee/EnumeratorsSpec.scala#L74-L83

    A2。不要定义Writeable[Option[String]],而是从Option[String]中提取String

    Ok.stream(
       Enumerator(Option("kiki"), Option("foo"), Option("bar")).map(_.get) >>> Enumerator.eof
    )
    

    【讨论】:

      猜你喜欢
      • 2014-10-04
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多