简体   繁体   English

基于计算值的scalaz流消耗流

[英]scalaz-stream consume stream based on computed value

I've got two streams and I want to be able to consume only one based on a computation that I run every x seconds. 我有两个流,并且我希望只能基于每x秒运行一次的计算消耗一个流。

I think I basically need to create a third tick stream - something like every(3.seconds) - that does the computation and then come up with sort of a switch between the other two. 我想我基本上需要创建第三个tick流-像every(3.seconds) -进行计算,然后在其他两个之间进行切换。

I'm kind of stuck here (and I've only just started fooling around with scalaz-stream). 我有点被困在这里(我只是刚刚开始使用scalaz-stream来开玩笑)。

Thanks! 谢谢!

There are several ways we can approach this problem. 有几种方法可以解决此问题。 One way to approach it is using awakeEvery . 一种解决方法是使用awakeEvery For concrete example, see here . 有关具体示例,请参见此处

To describe the example briefly, consider that we would like to query twitter in every 5 sec and get the tweets and perform sentiment analysis. 为了简短地描述示例,请考虑我们希望每5秒查询一次Twitter,并获取推文并进行情感分析。 We can compose this pipeline as follows: 我们可以如下组成该管道:

val source = 
    awakeEvery(5 seconds) |> buildTwitterQuery(query) through queryChannel flatMap {
        Process emitAll _ 
    }

Note that the queryChannel can be stated as follows. 注意, queryChannel可以表示如下。

def statusTask(query: Query): Task[List[Status]] = Task {
      twitterClient.search(query).getTweets.toList
}

val queryChannel: Channel[Task, Query, List[Status]] = channel lift statusTask

Let me know if you have any question. 如果您有任何问题,请告诉我。 As stated earlier, for the complete example, see this . 如前所述,有关完整示例,请参见this

I hope it helps! 希望对您有所帮助!

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

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