sliding window in RxJava

856 views
Skip to first unread message

Siqi Chen

unread,
Jul 30, 2015, 4:42:37 PM7/30/15
to RxJava
Hello,

I want to write a sliding window in RxJava..

For example,  a stream of events: t1, t2, t3, t4, t5 ,...

regular Window(2) will give me (t1, t2), (t3, t4), ...

But I need (t1, t2), (t2, t3), (t3, t4), (t4, t5), ....

Is there any code snippet someone can share?

Thanks

Ben Christensen

unread,
Jul 30, 2015, 4:46:46 PM7/30/15
to Siqi Chen, RxJava
window(2, 1) does overlapping. 

Ben Christensen
@benjchristensen

siqi chen

unread,
Jul 30, 2015, 5:17:33 PM7/30/15
to Ben Christensen, RxJava
Ben,

I can't find the window function that supports overlap like window(2, 1) here


You have any simple code ?


Also, I really want to do sliding window over a period of time. How to achieve this ?

Thanks
Siqi

Ben Christensen

unread,
Jul 30, 2015, 5:20:00 PM7/30/15
to siqi chen, RxJava

siqi chen

unread,
Jul 30, 2015, 5:22:59 PM7/30/15
to Ben Christensen, RxJava
Super cool. Thanks !!@@

Cédric Beust ♔

unread,
Jul 30, 2015, 5:24:17 PM7/30/15
to siqi chen, Ben Christensen, RxJava

Note that there’s also buffer(), and the difference between window() is subtle.

Here is an example from an article I wrote a while ago:

Observable.just(1, 2, 3, 4, 5)
    .buffer(3, 1)
    .subscribe { p("Received ${it}")

prints:

[main] Received [1, 2, 3]
[main] Received [2, 3, 4]
[main] Received [3, 4, 5]
[main] Received [4, 5]
[main] Received [5]

-- 
Cédric

Reply all
Reply to author
Forward
0 new messages