首先,使用Text 或ByteString 代替String 可以有很大帮助,而无需更改任何其他内容。
通常我不建议创建与Ord 不一致的Eq 实例。图书馆可以正确地依赖它,你永远不知道它会导致什么样的奇怪问题。 (例如,您确定Map 不使用Eq 和Ord 之间的关系吗?)
如果你根本不需要Eq 实例,你可以简单地定义
instance Eq BigThing where
x == y = compare x y == EQ
那么相等将与比较一致。不要求相等的值必须使所有字段都相等。
如果您需要一个比较所有字段的Eq 实例,那么您可以通过将BigThing 包装到newtype 中来保持一致,为其定义上述Eq 和Ord,并在您的当您需要根据name订购时的算法:
newtype BigThing' a b c = BigThing' (BigThing a b c)
instance Eq BigThing' where
x == y = compare x y == EQ
instance Ord BigThing' where
compare (BigThing b) (BigThing b') = compare (name b) (name b')
更新:既然你说任何排序都是可以接受的,你可以使用散列来发挥你的优势。为此,您可以使用 hashable 包。这个想法是您在数据创建时预先计算哈希值并在比较值时使用它们。如果两个值不同,则几乎可以肯定它们的哈希值会有所不同,并且您只比较它们的哈希值(两个整数),仅此而已。它可能看起来像这样:
module BigThing
( BigThing()
, bigThing
, btHash, btName, btSurname
)
where
import Data.Hashable
data BigThing = BigThing { btHash :: Int,
btName :: String,
btSurname :: String } -- etc
deriving (Eq, Ord)
-- Since the derived Eq/Ord instances compare fields lexicographically and
-- btHash is the first, they'll compare the hash first and continue with the
-- other fields only if the hashes are equal.
-- See http://www.haskell.org/onlinereport/derived.html#sect10.1
--
-- Alternativelly, you can create similar Eq/Ord instances yourself, if for any
-- reason you don't want the hash to be the first field.
-- A smart constructor for creating instances. Your module will not export the
-- BigThing constructor, it will export this function instead:
bigThing :: String -> String -> BigThing
bigThing nm snm = BigThing (hash (nm, snm)) nm snm
请注意,使用此解决方案,排序似乎是随机的,与字段没有明显关系。
您也可以将此解决方案与之前的解决方案结合使用。或者,您可以创建一个小模块,以使用其预先计算的散列包装任何类型(包装的值必须具有与其 Hashable 实例一致的 Eq 实例)。
module HashOrd
( Hashed()
, getHashed
, hashedHash
)
where
import Data.Hashable
data Hashed a = Hashed { hashedHash :: Int, getHashed :: a }
deriving (Ord, Eq, Show, Read, Bounded)
hashed :: (Hashable a) => a -> Hashed a
hashed x = Hashed (hash x) x
instance Hashable a => Hashable (Hashed a) where
hashWithSalt salt (Hashed _ x) = hashWithSalt salt x