[英]Haskell: negate a value of type Ordering
Is there a way to negate or invert a value of type Ordering? 有没有一种方法可以否定或求逆Ordering类型的值? Something like this:
像这样:
inv = not compare
If you are getting the Ordering from compare
, then you can change it to flip compare
: 如果要从
compare
获得Ordering,则可以将其更改为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: 或者,您可以将值包装在
Data.Ord
Down
, Data.Ord
是一种新的类型,可以用来颠倒事物的顺序:
> 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. 如果您不能或不想更改Ordering的来源,则需要一个函数将其反转,并且我不知道内置函数。 You can write the obvious pattern match, or since Ordering is itself ordered:
您可以编写明显的模式匹配,或者由于Ordering本身是有序的:
inv :: Ordering -> Ordering
inv = compare EQ
Yes it is contained in Data.Ord and is a type called Down
. 是的,它包含在Data.Ord中,并且是一个名为
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."
“如果一个具有与之关联的Ord实例,则比较由此包装的两个值将得到与它们正常排序顺序相反的结果。”
Reference: https://hackage.haskell.org/package/base-4.10.1.0/docs/Data-Ord.html 参考: https : //hackage.haskell.org/package/base-4.10.1.0/docs/Data-Ord.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.