首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带块和任务的Scala fs2流?

带块和任务的Scala fs2流?
EN

Stack Overflow用户
提问于 2016-12-21 19:30:34
回答 1查看 1K关注 0票数 0
代码语言:javascript
复制
  // Simulated external API that synchronously returns elements one at a time indefinitely.
  def externalApiGet[A](): A = ???

  // This wraps with the proper fs2 stream that will indefinitely return values.
  def wrapGetWithFS2[A](): Stream[Task, A] = Stream.eval(Task.delay(externalApiGet))

  // Simulated external API that synchronously returns "chunks" of elements at a time indefinitely.
  def externalApiGetSeq[A](): Seq[A] = ???

  // How do I wrap this with a stream that hides the internal chunks and just provides a stream of A values.
  // The following doesn't compile. I need help fixing this.
  def wrapGetSeqWithFS2[A](): Stream[Task, A] = Stream.eval(Task.delay(externalApiGetSeq))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-21 22:02:38

您需要将序列标记为Chunk,然后使用flatMap将流夷为平地。

代码语言:javascript
复制
def wrapGetSeqWithFS2[A](): Stream[Task, A] =
  Stream.eval(Task.delay(externalApiGetSeq()))
    .flatMap(Stream.emits)

(编辑以简化解决方案)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41270534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档