如何获得反应流中最后两个项目的滚动缓冲区? [英] How do I obtain a rolling buffer of the last two items emitted from a reactive stream?

查看:70
本文介绍了如何获得反应流中最后两个项目的滚动缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流发出数字x的流.我想要的是dx(x的差异),所以我需要一个滚动缓冲区,该缓冲区发出x_nx_(n-1)以便可以映射到dx = x_n - x_(n-1).在大理石图中,这看起来像...

I have a stream that emits numbers x. What I want is dx (difference in x) so I need a rolling buffer which emits x_n and x_(n-1) so I can map to dx = x_n - x_(n-1). In a marble diagram this would look like ...

SOURCE --A------B------C--------D------F--G-----

RESULT ---------AB-----BC-------CD-----DF-FG----

这对于滚动平均等其他操作非常方便.

This would be handy for other operations like rolling averages etc.

我检查了操作员文档,但似乎找不到任何类似的内容. sample有点接近,但与时间有关. buffer也很接近,但严格地将值排队,缓冲区之间没有重叠.

I have checked the operator docs but can't seem to find anything similar. sample is sort of close but is time dependent. buffer is also close but it strictly queues values with no overlap between the buffers.

我正在使用RxJS

推荐答案

RXJS 4

您甚至可能不需要buffer,一个简单的concatMap可能对您有用(当然,我不知道您的信息流的任何详细信息:

You maybe don't even need a buffer for this, a simple concatMap might work for you (of course I don't know any details of your stream:

observable = Rx.Observable.from(["A", "B", "C", "D", "E", "F"]);

observable
  .bufferWithCount(2, 1)
  .subscribe(all => {
    console.log(all);
});

此处

这篇关于如何获得反应流中最后两个项目的滚动缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆