Implemented the "Operator: Min and MinBy" and "Operator: Max and MaxBy"#478
Implemented the "Operator: Min and MinBy" and "Operator: Max and MaxBy"#478benjchristensen merged 4 commits intoReactiveX:masterfrom
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!