Skip to content

Conversation

@dobesv
Copy link
Contributor

@dobesv dobesv commented May 24, 2015

A combinator for Ord instances, that applies multiple Ords to the same object, in order.

Copy link
Member

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>).

Copy link
Contributor Author

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
.

Copy link
Member

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.

@jbgi
Copy link
Member

jbgi commented May 25, 2015

What about a 'fluent' instance method similar to java.util.Comparator.thenComparing(Comparator<? super T>) instead?

@jbgi
Copy link
Member

jbgi commented May 25, 2015

regarding subtyping, if B extends A, comap can be used to convert an Ord<A> to an Ord<B>.

@dobesv
Copy link
Contributor Author

dobesv commented May 25, 2015

That's a very nice idea.

On Mon, May 25, 2015, 1:51 AM JB Giraudeau notifications@github.com wrote:

What about a 'fluent' instance method similar to java.util.Comparator.thenComparing(Comparator<?
super T>) instead?


Reply to this email directly or view it on GitHub
#139 (comment)
.

@tonymorris
Copy link
Contributor

FWIW, comap is misnamed and should be called contramap. The current name is an accident of history.

I agree, use contramap, fmap/map and xmap instead of subtyping relations.

@mperry
Copy link
Contributor

mperry commented May 28, 2015

@jbgi and @dobesv, would you like to submit a new PR for something similar to Comparator.thenComparing? I am going to close this PR.

@mperry mperry closed this May 28, 2015
@mperry mperry added the invalid label May 30, 2015
@mperry mperry added this to the None milestone May 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants