Reputation: 2804
Is there a way to negate or invert a value of type Ordering? Something like this:
inv = not compare
Upvotes: 5
Views: 1445
Reputation: 11
Yes it is contained in Data.Ord and is a type called Down
.
I cite:
"If a has an Ord instance associated with it then comparing two values thus wrapped will give you the opposite of their normal sort order."
Reference: https://hackage.haskell.org/package/base-4.10.1.0/docs/Data-Ord.html
Upvotes: 0
Reputation: 2818
If you are getting the Ordering from compare
, then you can change
it to flip compare
:
> flip compare 3 4
GT
Or you can wrap the values in Down
from Data.Ord
, a newtype that
exists to reverse the ordering of things:
> compare (Down 3) (Down 4)
GT
If you can't or don't want to change the place that the Ordering comes from, you'll need a function to invert it and I don't know of a built-in one. You can write the obvious pattern match, or since Ordering is itself ordered:
inv :: Ordering -> Ordering
inv = compare EQ
Upvotes: 16