motoko-base icon indicating copy to clipboard operation
motoko-base copied to clipboard

utilities for tuple comparisons

Open ggreif opened this issue 1 year ago • 0 comments

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

ggreif avatar Dec 03 '23 16:12 ggreif