These are chat archives for ReactiveX/RxJava

14th
Sep 2015
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 14 2015 10:42

Hi, I have some flow which I can't resolve with provided operations. Or I just don't see something important.
I have sequence A which produces sequence B. Then later I combine them with combineLatest operator, and get two notifies on each A update. But I want one and latest.
Short illustration:

        Observable<Long> o1 = Observable.interval(1, TimeUnit.SECONDS).map(x -> x + 1);
        Observable<Long> o2 = o1.map(v -> -v);
        Observable.combineLatest(o1, o2, (v1, v2) -> "o1: " + v1 + ", o2: " + v2)
               .subscribe(System.out::println);

Provides output like this:

o1: 1, o2: -1
o1: 1, o2: -2
o1: 2, o2: -2
o1: 3, o2: -2
o1: 3, o2: -3
o1: 3, o2: -4
o1: 4, o2: -4
o1: 4, o2: -5
o1: 5, o2: -5
Dorus
@Dorus
Sep 14 2015 10:51
Try zip
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 14 2015 11:00

Ok. I missed one detail. B can be merged with C

       Observable<Long> a = Observable.interval(1, TimeUnit.SECONDS).map(x -> x + 1);
        Observable<Long> b = a.map(v -> -v);
        Observable<Long> c = Observable.interval(500, TimeUnit.MILLISECONDS).map(x -> x + 1000);
        Observable.zip(a, Observable.merge(b, c), (v1, v2) -> "o1: " + v1 + ", o2: " + v2).subscribe(System.out::println);

Then zip does wrong job too

Dorus
@Dorus
Sep 14 2015 11:04
zip a with b, combineLatest a with c, and then merge.
Alternative, you could use debounce to filter out the double results when a and b both yield, at the risk of losing c when it yields too soon after b.
oh, and i just realize, you need something like combineLatestRight. Does that exists in RxJava?
Dorus
@Dorus
Sep 14 2015 11:09
-> zip a with b, combineLatestRight a with c, and then merge.
Ilya Arkhanhelsky
@iarkhanhelsky
Sep 14 2015 11:12
works nice
thanks a lot!
Jason Martens
@jasonmartens
Sep 14 2015 22:28
Hello All, Is there a takeEvery(N) operation? For instance, to take every Nth element from a stream?
Maybe Scan?
Jason Martens
@jasonmartens
Sep 14 2015 22:42
I have a pre-existing set of timeseries data, and what I really want is Sample, but using the timestamps in the existing set
Dorus
@Dorus
Sep 14 2015 22:56
Use Where and then timestamp mod 1 min < 10s. Or if you want to base it on the previous accepted element, use scan to retain that element (and use distinct or so to filter our duplicates). You can also take 1 skip 9 with buffer(1,10).
Thats what i can come up with in 15 seconds ;)
Jason Martens
@jasonmartens
Sep 14 2015 22:58
phew, thanks for the ideas. ;-)
Dorus
@Dorus
Sep 14 2015 22:59
I'm not 100% sure if this is possible, but i suppose it should be, but you can also use window or groupBy to combine all elements within 1 minute and then only take the first N elements from the resulting sequences. Doing that with live data is easy, but i'm sure it's possible with pre-existing timestamp data too.