These are chat archives for ReactiveX/RxJava

12th
Oct 2015
David Stemmer
@weefbellington
Oct 12 2015 22:31
I made a Transformer to be able to do "lookback" without a behavior subject:
public class LookbackTransformer<T, R> implements Observable.Transformer<T, R> {

    private final Func2<T, T, R> mergeFunc;

    public LookbackTransformer(Func2<T, T, R> mergeFunc) {
        this.mergeFunc = mergeFunc;
    }

    @Override
    public Observable<R> call(Observable<T> source) {
        Observable<T> offsetSource = source.skip(1);
        return source.zipWith(offsetSource, mergeFunc);
    }
}
for example it allows you to take a stream returned by interval which emits [1, 2, 3, 4, 5] and map it to [(1,2),(2,3),(3,4),(4,5)]
is this a wise think to do or could there be unintended consequences?
Dorus
@Dorus
Oct 12 2015 22:34
This is different from Buffer(2,1) ?
David Stemmer
@weefbellington
Oct 12 2015 22:37
@Dorus wouldn't buffer yield something like [(1,2),(3,4),(5...)]?
Dorus
@Dorus
Oct 12 2015 22:37
not if you use skip 1 instead of skip 2
David Stemmer
@weefbellington
Oct 12 2015 22:39
ah interesting. I didn't know you could use it to create overlapping windows, neat
Dorus
@Dorus
Oct 12 2015 22:39
It is neat!
Works both on window() and buffer()
David Stemmer
@weefbellington
Oct 12 2015 22:39
the only downside would be you'd have to flatmap it but that's nbd
Dorus
@Dorus
Oct 12 2015 22:42
If i had to implement your Lookbacktransformer i would do something like Lookback(Observable<T> source Func2<T, T, R> merg) { return source.buffer(2,1).map(e => merg(e[0], e[1]));}
(Code is probably all wrong, not writing a whole lot of java lately)
David Stemmer
@weefbellington
Oct 12 2015 22:42
yeah that sounds about right to me
Dorus
@Dorus
Oct 12 2015 22:45
Actually, small problem is buffer gives a single element array before onComplete. Need to filter those out else you get a ArrayIndexOutOfBoundsException.
David Stemmer
@weefbellington
Oct 12 2015 22:46
you can just use flatmap with window
Dorus
@Dorus
Oct 12 2015 22:47
Mm actually, show me, i haven't been able to produce that yet.
David Stemmer
@weefbellington
Oct 12 2015 22:53
I'll have to do it later, I'm about to leave
Dorus
@Dorus
Oct 12 2015 22:57
no problem, leaving also :)
David Stemmer
@weefbellington
Oct 12 2015 22:58
is there anything wrong with zipping an observable with itself though? I thought it was quite clever
Dorus
@Dorus
Oct 12 2015 22:58
not really, but if subscribing twice has side effects, you could consider to publish it first
David Stemmer
@weefbellington
Oct 12 2015 22:59
well you're not subscribing in the transformer
Dorus
@Dorus
Oct 12 2015 23:02
I believe zip subscribes to both sources
Or does the transformer handle that? Again, something i'm not familiar with