gpt4 book ai didi

f# - F# 中字符串外的模式匹配类型

转载 作者:行者123 更新时间:2023-12-01 05:37:56 24 4
gpt4 key购买 nike

我正在开发一个函数,该函数可以匹配我在 f# 中的一些用户定义类型并将它们转换为字符串。部分代码如下所示:

let gsToString (gs : general_structure) : string = 
match gs with
| Date(Scattered(Eom(Ascending))) -> "Date(Scattered(Eom(Ascending)))"
| Date(Scattered(Eom(SameDate(dt)))) -> "Date(Scattered(Eom(SameDate(" + dt.ToString() + "))))"
| Number(AllNegative(Int1(Neither))) -> "Number(AllNegative(Int1(Neither)))"
| Number(AllNegative(Int1(SameInt(si)))) -> "Number(AllNegative(Int1(SameFloat(" + si.ToString() + "))))"

此函数中还有许多其他类型被匹配,但这些应该足以表达问题。此外,导致问题的类型是:

| SameDate of System.DateTime
| SameFloat of float

显然,执行第一个将我的 general_structure 类型转换为字符串的模式匹配函数非常简单。但是,我的下一个函数(需要稍后在代码中调用)出现问题,我需要将字符串表示重新转换回 general_structure。问题区域如下所示:

let stringToGS (str : string) : general_structure = 
match str with
| "Date(Scattered(Eom(Ascending)))" -> Date(Scattered(Eom(Ascending)))
| "Date(Scattered(Eom(SameDate(dt))))"-> Date(Scattered(Eom(SameDate(System.DateTime.Parse dt))))
| "Number(AllNegative(Int1(Neither)))" -> Number(AllNegative(Int1(Neither)))
| "Number(AllPositive(Float1(SameFloat(sf))))" -> Number(AllPositive(Float1(SameFloat((float) sf))))

虽然 stringToGS 函数中的第一种和第三种情况工作正常,但我无法找到将其他情况转换回其原始形式的方法。如果有任何方法可以在模式匹配语句中获取字符串(在本例中为 dt 和 fs)并以某种方式仅解析模式的那部分以返回不同的值(在本例中我试图使它们分别是 System.DateTimes 和 Floats)然后返回到它们的原始形式:

Date(Scattered(Eom(SameDate(dt))))
Number(AllPositive(Float1(SameFloat(sf))))

?如果有任何帮助,我将不胜感激。

编辑:

我能够通过对导致问题的情况使用 if 语句执行以下操作来解决问题:

if str.Contains("Scattered(Eom(SameDate")
then
let p1 = str.IndexOf(")")
let p2 = str.LastIndexOf("(")
let dt1 = str.Remove(p1)
let dt2 = dt1.Substring(p2 + 1)
let date = System.DateTime.Parse dt2
Date(Scattered(Eom(SameDate(date))))

然后,我可以对所有不包含嵌套数据的类型进行正常模式匹配。

最佳答案

如果类的数量有限并且您不想使用序列化库,您也可以使用事件模式:

open System

let (|RegexMatch|_|) pattern input =
let matches = System.Text.RegularExpressions.Regex.Matches(input, pattern)
if matches.Count = 1 then Some matches.[0].Groups.[1].Value
else None

type GeneralStructure =
| NoPayload
| DatePayload of DateTime
| StringPayload of string option

let toString = function
| NoPayload -> "NoPayload"
| DatePayload dt -> sprintf "DatePayload(%d)" <| dt.ToBinary()
| StringPayload None -> "StringPayload(None)"
| StringPayload (Some s) -> sprintf "StringPayload(Some(%s))" s

let fromString = function
| "NoPayload" -> NoPayload
| "StringPayload(None)" -> StringPayload None
| RegexMatch @"DatePayload\((.*)\)" dt -> DatePayload <| DateTime.FromBinary(Int64.Parse dt)
| RegexMatch @"StringPayload\(Some\((.*)\)\)" msg -> StringPayload <| Some msg
| o -> failwithf "Unknown %s %s" typeof<GeneralStructure>.Name o


let serialized = StringPayload <| Some "Foo" |> toString
let deserialized = fromString serialized

let serialized' = DatePayload DateTime.UtcNow |> toString
let deserialized' = fromString serialized'

// val serialized : string = "StringPayload(Some(Foo))"
// val deserialized : GeneralStructure = StringPayload (Some "Foo")
// val serialized' : string = "DatePayload(5247430828937321388)"
// val deserialized' : GeneralStructure = DatePayload 06.08.2015 18:04:10

请注意,正则表达式并非万无一失,我只是为了适应这些情况而编造的。

关于f# - F# 中字符串外的模式匹配类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31858545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com