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.
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.
I'm using RxJS
RXJS 4
You maybe don't even need a
buffer
for this, a simpleconcatMap
might work for you (of course I don't know any details of your stream:See live here
RXJS 5
RxJS has the
bufferCount
operator which works as follows ...overlap
is actually the sample frequency so for example in the above case ifoverlap = 2
then we would get the normalbuffer
behaviour.Two options that will work regardless of the version:
Better
Pairwise
bufferCount (or
bufferWithCount
)This does exist in RxJS 4 as well as RxJS 5
See both here
we can do
or if a hot observable:
or if a cold observable:
or easiest: