gpt4 book ai didi

f# - 从字符串数组中解析值

转载 作者:行者123 更新时间:2023-12-02 01:07:05 25 4
gpt4 key购买 nike

我正在尝试为已绘制到表单中的二维对象创建一个保存/加载函数。

type circle = { X : int; Y : int; Diameter : int; Brush : Brush}
type Square = { X : int; Y : int; Length : int; Height: int; Brush : Brush}

当我创建对象时,我将它们放入 2 个列表中,每种类型 1 个。我最初的想法是将这些对象读写到一个文本文件中,如下所示:

saveFile.Click.Add(fun _ ->
for c in listOfCircles do
myfile.WriteLine("Circle," + c.X.ToString() + "," + c.Y.ToString() + "," + c.Diameter.ToString() + "," + c.Brush.ToString())
for s in listOfSquares do
myfile.WriteLine("Square," + s.X.ToString() + "," + s.Y.ToString() + "," + s.Height.ToString() + "," + s.Length.ToString() + "," + s.Brush.ToString())
myfile.Close() // close the file

在文本文件中它看起来像这样

Circle,200,200,50,System.Drawing.SolidBrush
Square,50,55,45,55,System.Drawing.SolidBrush

从这里我想读取这些值,然后能够解析它们并通过将对象添加到列表并重新绘制它们来重新创建对象。

let readCircle =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; _|] when x = "Circle" -> Some (y, z, b)
| _ -> None )

let readSquare =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; a ; _|] when x = "Square" -> Some (y, z, b, a)
| _ -> None )

这些功能给了我

val readCircle : (string * string * string) [] = [|("200", "200", "50")|]
val readSquare : (string * string * string * string) [] = [|("50", "55", "45", "55")|]

我现在遇到的问题是我不确定如何从数组中获取值。下面是带有多个圆圈的示例。

val readCircle : (string * string * string) [] =  [|("200", "200", "50"); ("200", "200","50")|]

非常感谢任何有关如何从这里开始/如何解决此问题的想法或评论!问题摘要:我如何从数组中获取值并将它们放入例如我已经创建的添加函数中,请参见下文:

 listOfCircles.Add({ X = 200; Y = 200; Diameter = 50; Brush = Brushes.Black})

最佳答案

您可以使用 Array.map 转换您拥有的字符串元组数组,例如

[|("200", "200", "50"); ("200", "200","50")|]
|> Array.map (fun (x,y,d) -> {X = int32 x; Y = int32 y; Diameter = int32 d; Brush = Brushes.Black})

如果你在解析文件时转换为 circlesquare 可能会更清楚一些,那么你将得到一个 circle 数组> 或您可以直接添加到列表中的 square 数组。

let readCircle =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|t; x; y; d; _|] when t = "Circle" ->
Some {X = int32 x; Y = int32 y; Diameter = int32 d; Brush = Brushes.Red}
| _ -> None )

但是...如果您想进行更大的更改,您可以使用可区分的联合来表示您的形状,然后它们将共享一个共同类型的 Shape 并且您可以在相同的功能。

type Shape = 
| Circle of X : int * Y : int * Diameter : int * Brush : Brush
| Square of X : int * Y : int * Length : int * Height: int * Brush : Brush

let readShapes (data: string array) =
data
|> Array.choose (fun s ->
match s.Split ',' with
| [|t; x; y; d; _|] when t = "Circle" ->
Some (Circle(X = int32 x, Y = int32 y, Diameter = int32 d, Brush = Brushes.Red))
| [|t; x; y; l; h; _|] when t = "Square" ->
Some (Square(X = int32 x, Y = int32 y, Length = int32 l, Height = int32 h, Brush = Brushes.Red))
| _ -> None )

let listOfShapes = ResizeArray<_>()

let testInput = """
Circle,200,200,50,System.Drawing.SolidBrush
Square,50,55,45,55,System.Drawing.SolidBrush"""

testInput.Split('\n') // System.IO.File.ReadAllLines path
|> readShapes
|> Array.iter (listOfShapes.Add)

这会导致

val it : System.Collections.Generic.List<Shape> =
seq
[Circle (200,200,50,System.Drawing.SolidBrush {Color = Color [Red];});
Square (50,55,45,55,System.Drawing.SolidBrush {Color = Color [Red];})]

然后您可以使用模式匹配来绘制每种类型的形状

let drawShape shape =
match shape with
| Circle(x,y,d,b) ->
printfn "Pretend I just drew a circle at %d,%d with diameter %d." x y d
| Square(x,y,l,h,b) ->
printfn "Pretend I just drew a rectangle at %d,%d that was %d long and %d high." x y l h

listOfShapes |> Seq.iter drawShape

给予

Pretend I just drew a circle at 200,200 with diameter 50.
Pretend I just drew a rectangle at 50,55 that was 45 long and 55 high.

关于f# - 从字符串数组中解析值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21312019/

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