Skip to content

Add flatMap alias to mapMany #247

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 123 additions & 10 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import rx.operators.OperationConcat;
import rx.operators.OperationDefer;
import rx.operators.OperationDematerialize;
import rx.operators.OperationGroupBy;
import rx.operators.OperationFilter;
import rx.operators.OperationFinally;
import rx.operators.OperationGroupBy;
import rx.operators.OperationMap;
import rx.operators.OperationMaterialize;
import rx.operators.OperationMerge;
Expand Down Expand Up @@ -590,9 +590,11 @@ public void call(Object args) {

/**
* Returns a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
*
* @param subject the subject to push source elements into.
* @param <R> result type
*
* @param subject
* the subject to push source elements into.
* @param <R>
* result type
* @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
*/
public <R> ConnectableObservable<R> multicast(Subject<T, R> subject) {
Expand Down Expand Up @@ -1172,6 +1174,8 @@ public R call(T t1) {
* and then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param sequence
Expand All @@ -1186,6 +1190,7 @@ public R call(T t1) {
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item emitted by the source Observable and merging the results of
* the Observables obtained from this transformation
* @see {@link #flatMap(Observable, Func1)}
*/
public static <T, R> Observable<R> mapMany(Observable<T> sequence, Func1<T, Observable<R>> func) {
return create(OperationMap.mapMany(sequence, func));
Expand Down Expand Up @@ -1351,6 +1356,62 @@ public static <T> Observable<T> finallyDo(Observable<T> source, Action0 action)
return create(OperationFinally.finallyDo(source, action));
}

/**
* Creates a new Observable sequence by applying a function that you supply to each object in the
* original Observable sequence, where that function is itself an Observable that emits objects,
* and then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item emitted by the source Observable, generating a
* Observable
* @param <T>
* the type emitted by the source Observable
* @param <R>
* the type emitted by the Observables emitted by <code>func</code>
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item emitted by the source Observable and merging the results of
* the Observables obtained from this transformation
* @see {@link #mapMany(Observable, Func1)}
*/
public static <T, R> Observable<R> flatMap(Observable<T> sequence, Func1<T, Observable<R>> func) {
return mapMany(sequence, func);
}

/**
* Creates a new Observable sequence by applying a function that you supply to each object in the
* original Observable sequence, where that function is itself an Observable that emits objects,
* and then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item emitted by the source Observable, generating a
* Observable
* @param <T>
* the type emitted by the source Observable
* @param <R>
* the type emitted by the Observables emitted by <code>func</code>
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item emitted by the source Observable and merging the results of
* the Observables obtained from this transformation
* @see {@link #mapMany(Observable, Func1)}
*/
public static <T, R> Observable<R> flatMap(Observable<T> sequence, final Object func) {
return mapMany(sequence, func);
}

/**
* Groups the elements of an observable and selects the resulting elements by using a specified function.
*
Expand Down Expand Up @@ -2088,11 +2149,15 @@ public static <T> Iterable<T> mostRecent(Observable<T> source, T initialValue) {

/**
* Returns a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
*
* @param source the source sequence whose elements will be pushed into the specified subject.
* @param subject the subject to push source elements into.
* @param <T> source type
* @param <R> result type
*
* @param source
* the source sequence whose elements will be pushed into the specified subject.
* @param subject
* the subject to push source elements into.
* @param <T>
* source type
* @param <R>
* result type
* @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
*/
public static <T, R> ConnectableObservable<R> multicast(Observable<T> source, final Subject<T, R> subject) {
Expand All @@ -2101,7 +2166,7 @@ public static <T, R> ConnectableObservable<R> multicast(Observable<T> source, fi

/**
* Returns the only element of an observable sequence and throws an exception if there is not exactly one element in the observable sequence.
*
*
* @param that
* the source Observable
* @return The single element in the observable sequence.
Expand Down Expand Up @@ -2670,6 +2735,48 @@ public Boolean call(T t1) {
});
}

/**
* Creates a new Observable sequence by applying a function that you supply to each item in the
* original Observable sequence, where that function is itself an Observable that emits items, and
* then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param func
* a function to apply to each item in the sequence, that returns an Observable.
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item in the input sequence and merging the results of the
* Observables obtained from this transformation.
* @see {@link #mapMany(Func1)}
*/
public <R> Observable<R> flatMap(Func1<T, Observable<R>> func) {
return mapMany(func);
}

/**
* Creates a new Observable sequence by applying a function that you supply to each item in the
* original Observable sequence, where that function is itself an Observable that emits items, and
* then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param callback
* a function to apply to each item in the sequence that returns an Observable.
* @return an Observable that emits a sequence that is the result of applying the transformation'
* function to each item in the input sequence and merging the results of the
* Observables obtained from this transformation.
* @see {@link #mapMany(Object)}
*/
public <R> Observable<R> flatMap(final Object callback) {
return mapMany(callback);
}

/**
* Filters an Observable by discarding any of its emissions that do not meet some test.
* <p>
Expand Down Expand Up @@ -2805,13 +2912,16 @@ public R call(T t1) {
* then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param func
* a function to apply to each item in the sequence, that returns an Observable.
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item in the input sequence and merging the results of the
* Observables obtained from this transformation.
* @see {@link #flatMap(Func1)}
*/
public <R> Observable<R> mapMany(Func1<T, Observable<R>> func) {
return mapMany(this, func);
Expand All @@ -2823,13 +2933,16 @@ public <R> Observable<R> mapMany(Func1<T, Observable<R>> func) {
* then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* Note: mapMany and flatMap are equivalent.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param callback
* a function to apply to each item in the sequence that returns an Observable.
* @return an Observable that emits a sequence that is the result of applying the transformation'
* function to each item in the input sequence and merging the results of the
* Observables obtained from this transformation.
* @see {@link #flatMap(Object))}
*/
public <R> Observable<R> mapMany(final Object callback) {
@SuppressWarnings("rawtypes")
Expand Down