Add Equal 'type class'

Issue #40 resolved
Jean-Baptiste Giraudeau created an issue

I think fugue should provide a Equal type class similar to https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Equal.java

  • It would provide additional type-safety over Object.equals
  • It would allow to test for equality of objects from classes that do not implement equals or implement it not the way you want. (eg. it would permit to compare Iterable or compare BigDecimal via compareTo() instead of equals()...)

Equal is also a quasi-requirement for sane property based testing (monoids and optics).

Maybe also Hash and Show type class sould be considered for addition. But Equal is the most useful.

Comments (5)

  1. Jean-Baptiste Giraudeau reporter

    Something like:

    @FunctionalInterface
    public interface Equal<A> extends Function<A, Predicate<A>>, BiPredicate<A, A> {
    
      @Override default boolean test(A a1, A a2) {
        return apply(a1).test(a2);
      }
    }
    
  2. Anund McKague

    Does this type class specify the level of equality that makes sense or is it up to the implementations to decide on things like value vs referential equality?

    Can you expand on the type of code you'd like to write that you can write with a comparator (or helper methods like mkPred(Comparator<A>c , A a): Predicate<A>)?

  3. Jean-Baptiste Giraudeau reporter

    Equal is for mathematical equality, in that sense: 1.00 == 1.0 Also useful for types that cannot implement Object.equals in any meaningful way. eg for types like Iterable<BigDecimal>, I would need something like: static <A> Equal<Iterable<A>> iterableEqual(Equal<A> aEqual)

  4. Jean-Baptiste Giraudeau reporter

    Equal may also be useful for floating point arithmetic. Since double addition is not associative in general - (0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3) return false - to want to use algorithm that abstract over Monoids with Double then you may need to define equality within an approximation that is 'morally' acceptable for your program. Hence a customized Equal<Double>.

    But this may be a minor use case for an Equal typeclass.

    The additional type-safety and possibility to define equality for eg. Iterable is what make it really useful. FJ, scalaz, haskell concur on this.

  5. Log in to comment