我有Job类型,相等被定义为Job的ID相等.永远不应该有两个具有相同ID的作业.虽然它们不具有可比性,但是一项工作不多于或少于另一项工作,只是相同或不相同.
type JobId = JobId of string
[<CustomEquality; NoComparison>]
type Job = {
Id: JobId
} with
interface System.IEquatable<Job> with
member x.Equals y = x.Id = y.Id
type Resource = {
Id: string
Capacity: float
Usage: Map<Job,float>
}
Run Code Online (Sandbox Code Playgroud)
虽然Map需要比较.
Map需要进行比较?IDictionary但我试图保持功能.)在内部,F#Map被实现为平衡二叉树(具体来说,AVL 树),这需要比较其键类型才能决定任何项目属于树中的位置。对于不需要比较的哈希映射,PersistentHashMapFSharpx.Collections 中的类型可能就是您想要的。