-
Notifications
You must be signed in to change notification settings - Fork 253
Add "chain" combinator for Ord #139
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
Conversation
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.
In my understanding, functionaljava try to avoid the use of variance annotations (and subtyping as a whole), except for methods specifically created to deal with subtyping, like fj.Function.vary(F<? super A, ? extends B>).
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.
Without the variance annotation you can't directly reuse an Ord for a
superclass in a subclass, I think. This is exactly why I created this
chain function, was to reuse Ord in subclasses.
On Mon, May 25, 2015, 1:43 AM JB Giraudeau notifications@github.com wrote:
In core/src/main/java/fj/Ord.java
#139 (comment)
:@@ -514,4 +514,21 @@ public int compare(A o1, A o2) {
public Comparator toComparator() {
return new OrdComparator();
}
+
- /**
- * Given two Ord instances that compare the given type of object, chain them together so
- * that the result of the first is returned if it is not EQ, otherwise the result
- * of the second is returned. This can be used to reuse an Ord between subclasses
- * or implementations of an interface.
- * @return An Ord instance combining the two provided Ord instances
- */
- public static Ord chain(Ord<? super A> ord1, Ord<? super A> ord2) {
In my understanding, functionaljava try to avoid the use of variance
annotations (and subtyping as a whole), except for methods specifically
created to deal with subtyping, like fj.Function.vary(F<? super A, ?
extends B>).—
Reply to this email directly or view it on GitHub
https://github.com/functionaljava/functionaljava/pull/139/files#r30969203
.
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 cannot 'directly' reuse it but you can still use comap which should do what you want at a small cost:
Ord<Date> dateOrd = Ord.comparableOrd();
Ord<Timestamp> timestampOrd = dateOrd.comap(t -> t);functionaljava does not support variance annotation because composition is usually preferred (over inheritance) when functional-programming.
|
What about a 'fluent' instance method similar to |
|
regarding subtyping, if |
|
That's a very nice idea. On Mon, May 25, 2015, 1:51 AM JB Giraudeau notifications@github.com wrote:
|
|
FWIW, I agree, use |
A combinator for Ord instances, that applies multiple Ords to the same object, in order.