简体   繁体   English

如何使用Scalaz-Stream创建流?

[英]How to create a stream with Scalaz-Stream?

It must be damn simple. 它一定很简单。 But for some reason I cannot make it work. 但是由于某种原因,我无法使其正常工作。

  • If I do io.linesR(...) , I have a stream of lines of the file, it's ok. 如果我执行io.linesR(...) ,那么文件行就可以了。
  • If I do Processor.emitAll() , I have a stream of pre-defined values. 如果我执行Processor.emitAll() ,那么我会有一个预定义值流。 It also works. 它也可以。

But what I actually need is to produce values for scalaz-stream asynchronously (well, from Akka actor). 但是我真正需要的是异步生成scalaz-stream的值(好吧,来自Akka演员)。

I have tried: 我努力了:

  • async.unboundedQueue[String]
  • async.signal[String]

Then called queue.enqueueOne(...).run or signal.set(...).run and listened to queue.dequeue or signal.discrete . 然后调用queue.enqueueOne(...).runsignal.set(...).run并监听queue.dequeuesignal.discrete Just with .map and .to . 仅使用.map.to With an example proved to work with another kind of stream -- either with Processor or lines from the file. 通过一个示例证明可以与另一种流一起使用-使用处理器或文件中的行。

What is the secret? 有什么秘诀? What is the preferred way to create a channel to be streamed later? 创建稍后流式传输的频道的首选方式是什么? How to feed it with values from another context? 如何用另一个上下文中的值来提供它?

Thanks! 谢谢!

If the values are produced asynchronously but in a way that can be driven from the stream, I've found it easiest to use the "primitive" await method and construct the process "by hand". 如果值是异步生成的,但可以通过流中的方式来生成,那么我发现使用“原始” await方法并“手动”构造过程是最简单的。 You need an indirectly recursive function: 您需要一个间接递归函数:

def processStep(v: Int): Process[Future, Int] =
  Process.emit(v) ++ Process.await(myActor ? NextValuePlease())(w => processStep(w))

But if you need a truly async process, driven from elsewhere, I've never done that. 但是,如果您需要一个真正的异步流程(由其他地方来驱动),则我从来没有这样做。

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

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