getOrd<A, B> function

Ord<Either<A, B>> getOrd<A, B>(
  1. Ord<A> leftOrd,
  2. Ord<B> rightOrd
)

Returns an Ord instance for comparing Either values based on the provided orderings for the Left and Right types.

This function wraps around EitherOrd to provide a convenient way to get an ordering for Either values. The behavior of comparison follows the same rules as described in EitherOrd.

Example usage:

final intOrder = Ord<int>((a, b) => a.compareTo(b));
final strOrder = Ord<String>((a, b) => a.compareTo(b));

final eitherOrder = getOrd(intOrder, strOrder);

final e1 = Left<int, String>(3);
final e2 = Left<int, String>(5);
final e3 = Right<int, String>("apple");
final e4 = Right<int, String>("banana");

print(eitherOrder.compare(e1, e2)); // -1 because 3 < 5
print(eitherOrder.compare(e3, e4)); // -1 because "apple" < "banana"
print(eitherOrder.compare(e1, e3)); // -1 because Left always comes before Right
  • leftOrd: The ordering to be used for comparing Left values.
  • rightOrd: The ordering to be used for comparing Right values.

Returns: An instance of EitherOrd with the provided orderings.

Implementation

Ord<Either<A, B>> getOrd<A, B>(Ord<A> leftOrd, Ord<B> rightOrd) {
  return EitherOrd<A, B>(leftOrd, rightOrd);
}