-
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
MathObservable #942
MathObservable #942
Conversation
Moving the average/sum/min/max functionality to the MathObservable similar to StringObservable.
RxJava-pull-requests #879 SUCCESS |
Hi, being a fan of method chaining I'd like to see more Operator implementations of for example the math operations and many more. Moreover, I'd like to have an idiom tested and confirmed by you experts to do this easily for my own stuff. To that end do you think it would be of value to include in the library something like the class below? One possibility would be to add the toOperator static method to the Observable class. import rx.Observable;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Func1;
import rx.subjects.PublishSubject;
/**
* Converts an Operation (a function converting one Observable into another)
* into an {@link Operator}.
*
* @param <R>
* to type
* @param <T>
* from type
*/
public class OperatorFromOperation<R, T> implements Operator<R, T> {
public static <R, T> Operator<R, T> toOperator(Func1<Observable<T>, Observable<R>> operation) {
return new OperatorFromOperation<R, T>(operation);
}
/**
* The operation to convert.
*/
private final Func1<Observable<T>, Observable<R>> operation;
/**
* Constructor.
*
* @param operation
* to be converted into {@link Operator}
*/
public OperatorFromOperation(Func1<Observable<T>, Observable<R>> operation) {
this.operation = operation;
}
@Override
public Subscriber<? super T> call(Subscriber<? super R> subscriber) {
final PublishSubject<T> subject = PublishSubject.create();
Subscriber<T> result = createSubscriber(subject);
subscriber.add(result);
operation.call(subject).subscribe(subscriber);
return result;
}
/**
* Creates a subscriber that passes all events on to the subject.
*
* @param subject
* receives all events.
* @return
*/
private static <T> Subscriber<T> createSubscriber(final PublishSubject<T> subject) {
return new Subscriber<T>() {
@Override
public void onCompleted() {
subject.onCompleted();
}
@Override
public void onError(Throwable e) {
subject.onError(e);
}
@Override
public void onNext(T t) {
subject.onNext(t);
}
};
}
} |
What could you do with operation that couldn't be done with |
An operation acts upon the observable as a whole not on the individual
|
I don't understand what use cases are trying to be solved by that code, can you provide some examples please? |
So for instance if I want to use sumInteger in the middle of a chain of I make an operator form of it by implementing a call to sumInteger in a I should mention that method chained calls are important to me because the I'm still new to rxjava so let me know if I'm missing something obvious On 11 March 2014 03:26, Ben Christensen [email protected] wrote:
|
To add an explicit example I've found the method RxUtil.toOperator as private final Operator<Integer,Integer> SUM_INTEGER = Observable.from(asList(1,2,3)).lift(SUM_INTEGER); On 11 March 2014 08:07, Dave Moten [email protected] wrote:
|
I think perhaps I should simplify my original question. Suppose I have a public static <R,T> Observable something(Observable source) { What is the simplest way to make an Operator out of it so that I can use it On 12 March 2014 11:35, Dave Moten [email protected] wrote:
|
We should add an overload for lift that takes an Observable => Observable function. Very much like http://msdn.microsoft.com/en-us/library/hh229147(v=vs.103).aspx. This is on the TODO list, or you can try to build it yourself, and submit a pull request. Note it should allow subscribing more than once in the lambda, hence "publish" in .NET. |
+1 |
I'll make a pull request with the lift method overload as suggested. On 13 March 2014 13:41, Shixiong Zhu [email protected] wrote:
|
I think we already have that publish() method: Observable.java:5428 which relies on OperationMulticast. If you could reimplement it to OperatorMulticast, we would gain a lot of operators. |
I'm a bit lost about the reference to publish() method that started with headinthebox's comment. I am not talking about adding a publish method. I am talking about adding a toOperator method or overload on lift method that does the same thing. |
@benjchristensen what do you think about the addition of an overload for lift that takes a Func1<Observable,Observable>? Should I go ahead with a pull request? |
Moving the average/sum/min/max functionality to the MathObservable similar to StringObservable.
Similar to the rxjava-string module this is seeking to achieve the goal of keeping rxjava-core focused on core functionality.