dfinity / motoko-base

The Motoko base library
Apache License 2.0
483 stars 99 forks source link

utilities for tuple comparisons #602

Open ggreif opened 11 months ago

ggreif commented 11 months ago

I came up with these:

    type Comp = { #less; #equal; #greater };
    // Comparison combinators
    public func compare2<A, B>(ca : (A, A) -> Comp, cb : (B, B) -> Comp)
      : ((A, B), (A, B)) -> Comp =
      func((a1 : A, b1 : B), (a2 : A, b2 : B)) : Comp =
        switch (ca(a1, a2)) {
          case (#equal) cb(b1, b2);
          case other other
        };

    public func compare3<A, B, C>(ca : (A, A) -> Comp,
                                  cbc : ((B, B) -> Comp, (C, C) -> Comp))
      : ((A, (B, C)), (A, (B, C))) -> Comp
      = compare2(ca, compare2 cbc);

We could have a helper module giving these a home?

Tracked here too: https://dfinity.atlassian.net/browse/LANG-331