-
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
Implemented the "Operator: Min and MinBy" and "Operator: Max and MaxBy" #478
Conversation
RxJava-pull-requests #404 SUCCESS |
|
Is the following solution OK? public static <T extends Comparable<T>> Observable<T> min(
Observable<T> source) {
return minMax(source, -1);
}
public static <T extends Comparable<T>> Observable<T> max(
Observable<T> source) {
return minMax(source, 1);
}
public static <T extends Comparable<T>> Observable<T> minMax(
Observable<T> source, final long flag) {
return source.reduce(new Func2<T, T, T>() {
@Override
public T call(T acc, T value) {
if (flag * acc.compareTo(value) > 0) {
return acc;
}
return value;
}
});
}
Another solution is: public static <T extends Comparable<T>> Observable<T> min(
Observable<T> source) {
return minMax(source, true);
}
public static <T extends Comparable<T>> Observable<T> max(
Observable<T> source) {
return minMax(source, false);
}
public static <T extends Comparable<T>> Observable<T> minMax(
Observable<T> source, final boolean isMin) {
return source.reduce(new Func2<T, T, T>() {
@Override
public T call(T acc, T value) {
if (isMin) {
if (acc.compareTo(value) < 0) {
return acc;
}
} else {
if (acc.compareTo(value) > 0) {
return acc;
}
}
return value;
}
});
} @samuelgruetter , do you have other better solution? |
I like both of these two solutions, with a slight preference for the first one ;-) A third solution would be to implement max using min and wrapping the given comparator such that it inverts the ordering, but I think the first solution is the best. Another issue: What if there are several minimal elements? Does min return the first of them, the last of them, or is it unspecified? This should be documented. |
@samuelgruetter , Thanks for your review. I used the '+1/-1' way to implement it and also updated the document. |
RxJava-pull-requests #407 SUCCESS |
Looks good @zsxwing and thanks @samuelgruetter for the review. Some nice use of generics in those signatures! |
Implemented the "Operator: Min and MinBy" and "Operator: Max and MaxBy"
Implemented the "Operator: Min and MinBy" and "Operator: Max and MaxBy"
Add find() method that will only return an existing object in a Registry but will not create a new one.
Hi, this PR implemented the
Operator: Min and MinBy
#63 andOperator: Max and MaxBy
#61. Every operator has 2 variants, one forComparable
, another forComparator
. Please take a look. Thanks!