F# -> 为 HashSet<'a> 实现 IComparable

2023-11-30

是否有可能以某种方式实施IComparable for a HashSet<'a>?原因是我声明了以下记录:

[<StructuralComparison>]
type Category = { 
    mutable Id: string; 
    Name: string; 
    SavePath: string;
    Tags: HashSet<Tag> }

and Tag = { Tag:string; }

如您所见,然后标签中Category记录的类型HashSet<Tag>- 为了将一系列类别映射到地图,我需要实现IComparable不知怎的......否则它只会导致:

结构体、记录或联合类型 “类别”有 'StructuralComparison' 属性但是 组件类型“HashSet”的作用 不满足“比较”

请注意,除了 a 之外我不能使用任何其他东西HashSet<'a>因为我正在使用的数据库根本不理解任何尖锐的列表。


我假设你想要比较并等同Categorys 通过仅采取Id, Name, and SavePath考虑到(按顺序),使记录的行为就像Tags不在场:

open System
open System.Collections.Generic

[<CustomComparison; CustomEquality>]
type Category =
    { mutable Id : string;
      Name       : string;
      SavePath   : string;
      Tags       : HashSet<Tag> }
    member private this.Ident = this.Id, this.Name, this.SavePath
    interface IComparable<Category> with
        member this.CompareTo other =
            compare this.Ident other.Ident
    interface IComparable with
        member this.CompareTo obj =
            match obj with
              | null                 -> 1
              | :? Category as other -> (this :> IComparable<_>).CompareTo other
              | _                    -> invalidArg "obj" "not a Category"
    interface IEquatable<Category> with
        member this.Equals other =
            this.Ident = other.Ident
    override this.Equals obj =
        match obj with
          | :? Category as other -> (this :> IEquatable<_>).Equals other
          | _                    -> false
    override this.GetHashCode () =
        hash this.Ident

and Tag = { Tag : string; }

但是,如果您想比较Name并等于Id然后考虑以下事项:

open System
open System.Collections.Generic

[<CustomComparison; CustomEquality>]
type Category =
    { mutable Id : string;
      Name       : string;
      SavePath   : string;
      Tags       : HashSet<Tag> }
    interface IComparable<Category> with
        member this.CompareTo { Name = name } =
            this.Name.CompareTo name
    interface IComparable with
        member this.CompareTo obj =
            match obj with
              | null                 -> 1
              | :? Category as other -> (this :> IComparable<_>).CompareTo other
              | _                    -> invalidArg "obj" "not a Category"
    interface IEquatable<Category> with
        member this.Equals { Id = id } =
            this.Id = id
    override this.Equals obj =
        match obj with
          | :? Category as other -> (this :> IEquatable<_>).Equals other
          | _                    -> false
    override this.GetHashCode () =
        this.Id.GetHashCode ()

and Tag = { Tag : string; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

F# -> 为 HashSet<'a> 实现 IComparable 的相关文章

  • F# 静态成员类型约束

    我正在尝试定义一个函数 factorize 它使用类似于 Seq sum 的结构类型约束 需要静态成员 Zero One 和 以便它可以与 int long bigint 等一起使用 似乎无法获得正确的语法 并且无法找到有关该主题的大量资源
  • 基于函数签名的模式匹配

    在 F 中 您可以对函数签名进行模式匹配 我想用一个函数来装饰多个函数 该函数测量函数的执行情况并调用 statsd 我当前的功能是 let WrapFunctionWithPrefix metrics Metric Client IRec
  • F# 使用 while 循环

    我有一个数据读取器 我想从中返回行集合 在阅读了一天的书籍后 我无法找到在 f 中执行此操作的最佳方法 我可以在 F 中以正常的 C 方式进行操作 但这不是我使用 f 的原因 这就是我想要实现的目标 let values while rea
  • 如何使用 WebSharper 在服务器上生成 Google Visualizations 数据

    我的目标是能够在服务器上为 Google Visualizations 生成数据 然后将其作为 java 脚本传递给客户端 以便可以将其呈现为折线图 我下面的示例可以正确编译 但在浏览器中呈现时会产生错误 在服务器上构建 DataCommo
  • 使用 FParsec 解析 int 或 float

    我正在尝试使用 FParsec 解析文件 该文件由 float 或 int 值组成 我面临两个问题 无法找到好的解决方案 1 Both pint32 and pfloat将成功解析相同的字符串 但给出不同的答案 例如pint32将返回3解析
  • 将可区分的联合传递给 InlineData 属性

    我正在尝试对一个解析器进行单元测试 该解析器解析字符串并返回相应的抽象语法树 表示为可区分的联合 我认为使用 Xunit Extensions 属性会非常紧凑InlineData将所有测试用例堆叠在一起
  • F# 核心库源代码有一个用于将元组编译为结构的标志,但我无法使其工作

    这是后续问题这个提议 https fslang uservoice com forums 245727 f language suggestions 6148669 short tuples compiled as structs up t
  • 使用反射创建 Action<'T> 的实例

    我将如何创建一个实例Action lt T gt 使用反射 这是我所拥有的 let makeAction typ Type f T gt unit let actionType typedefof
  • F# 生成日期序列/数组

    在 F 中我可以轻松做到 let a 1 10 那我为什么不能做 let a DateTime Parse 01 01 2012 let b DateTime Parse 01 01 2020 let dateList a b 它给出了一个
  • F# 中的递归对象?

    这段 F 代码 let rec reformat new EventHandler fun gt b TextChanged RemoveHandler reformat b gt ScrollParser rewrite contents
  • F# 方法返回 null 而不是 Option

    我开发F 应用 net 4 6 1 on VS2015 我有方法 type CommonHelper static member SideEffectOnNull act x if x null then act x else x stat
  • 使用列表匹配绑定值(没有编译器警告)

    假设我有一个需要一些时间的函数int参数 但在其中我将使用float32 我不想使用float32 i无处不在的功能 相反 我想这样做 let x float32 x let y float32 y let w float32 w let
  • 如何在 F# 中使用 LINQ 更新数据库中的表?

    我看过很多有关如何查询数据库的示例 但没有看到有关如何更新记录的示例 下面是我编写的用于检索表的简单代码 但有人可以解释一下如何修改字段 例如lastActiveDate 并更新数据库上的表 谢谢你 周日 open System open
  • 在构建服务器上安装 F# 4.1 SDK

    我已在 PC 上安装了支持 F 的 Visual Studio 2017 并且 MSBuild 目标位于C Program Files x86 Microsoft Visual Studio 2017 Enterprise MSBuild
  • F# 会自动内联一些函数,即使它们没有标记为“inline”,这是有意的吗?

    看起来 F 会自动内联一些函数 即使它们没有标记为 内联 let a x x 3 let b x x x let funB x y if x gt y then 3 else 1 let funC x let s a x let c fun
  • 如何在 F# 中实现返回 void 的接口成员

    想象一下 C 中的以下接口 interface IFoo void Bar 我如何在 F 中实现这一点 我在 30 分钟的在线搜索中找到的所有示例都仅显示具有返回类型的示例 我认为这在函数式风格中更常见 但在这种情况下我无法避免 这是我到目
  • F# 查询,按单列对多个值进行分组

    我有一个 F sql 查询 需要对每组中的两列求和 let financials query for data in dbData do groupValBy data earning data losses data store into
  • F# 如何标记用户输入:分隔数字、单位、单词?

    我对 F 相当陌生 但最近几周一直在阅读参考资料 我希望处理用户提供的输入字符串 识别并分隔组成元素 例如 对于此输入 XYZ 酒店 6 晚 220 欧元 晚 加17 5 的税 输出应该类似于元组列表 XYZ 字 酒店 字 6 数字 夜晚
  • 可移植类库和.NET ConcurrentDictionary

    看着http msdn microsoft com en us library dd287191 v vs 110 aspx http msdn microsoft com en us library dd287191 v vs 110 a
  • F# 在生成和终止进程方面真的比 Erlang 更快吗?

    更新 这个问题包含一个错误 使得基准测试毫无意义 我将尝试一个更好的基准来比较 F 和 Erlang 的基本并发功能 并在另一个问题中查询结果 我正在尝试了解 Erlang 和 F 的性能特征 我发现 Erlang 的并发模型非常有吸引力

随机推荐