-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MergeMap with Iterable and resultSelector overloads #736
Conversation
Forgot an overload. |
RxJava-pull-requests #650 SUCCESS |
RxJava-pull-requests #651 SUCCESS |
*/ | ||
public <U, R> Observable<R> mergeMap(Func1<? super T, ? extends Observable<? extends U>> collectionSelector, | ||
Func2<? super T, ? super U, ? extends R> resultSelector) { | ||
return create(OperationFlatMap.flatMap(this, collectionSelector, resultSelector)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks it can be implemented by a simple class Pair
, for example:
public <U, R> Observable<R> mergeMap(final Func1<? super T, ? extends Observable<? extends U>> collectionSelector,
final Func2<? super T, ? super U, ? extends R> resultSelector) {
return flatMap(new Func1<T, Observable<Pair<T, U>>>() {
@Override
public Observable<Pair<T, U>> call(final T t) {
return collectionSelector.call(t).map(new Func1<U, Pair<T, U>>() {
@Override
public Pair<T, U> call(U u) {
return new Pair<T, U>(t, u);
}
});
}
}).map(new Func1<Pair<T, U>, R>() {
@Override
public R call(Pair<T, U> pair) {
return resultSelector.call(pair._1, pair._2);
}
});
}
private static class Pair<T1, T2> {
T1 _1;
T2 _2;
Pair(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to introduce Pair for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use any better class to replace Pair. However, I think we do not need to reimplement flatMap
again here.
Looks these operators can be implemented by composing the existing operators. |
I agree there is likely some simplification we can do on this, but I'm going to merge as the public APIs look correct and unit tests are good. We can iterate on the implementation internally. |
MergeMap with Iterable and resultSelector overloads
Listed in #653.