Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flatZip in RxJava

I'm zipping multiple Observables together and then transforming them in a way that results in an Observable:

final Observable<Observable<M>> result = Observable.zip(obs1, obs2, transformFunc);

What I'd like to be able to do is:

final Observable<M> result = Observable.flatZip(obs1, obs2, transformFunc);

What's the cleanest way to do this, given flatZip doesn't exist (maybe I should submit one). At the moment I'm having to flatMap the result in on itself.

like image 640
Ben Smith Avatar asked May 18 '14 12:05

Ben Smith


1 Answers

public class RxHelper {

    public static <T1, T2, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, final Func2<? super T1, ? super T2, Observable<? extends R>> zipFunction) {
        return Observable.merge(Observable.zip(o1, o2, zipFunction));
    }

    public static <T1, T2, T3, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Func3<? super T1, ? super T2, ? super T3, Observable<? extends R>> zipFunction) {
        return Observable.merge(Observable.zip(o1, o2, o3, zipFunction));
    }

    public static <T1, T2, T3, T4, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Func4<? super T1, ? super T2, ? super T3, ? super T4, Observable<? extends R>> zipFunction) {
        return Observable.merge(Observable.zip(o1, o2, o3, o4, zipFunction));
    }
}
like image 162
esneider Avatar answered Nov 04 '22 22:11

esneider