F # - Health Check and Parameters

I'm new to F #, so it's hard for me to change my mindset after many years of C # / Java OOP.

I have an event handler MyForm.SelectFile(filePath:String)that opens a dialog box and allows me to select a file to read. After selecting the file, it is called Parser.LoadFile(filePath:String):

static member LoadFile(filePath:String) =
    if not <| ZipFile.IsZipFile(filePath) then
        failwith "invalid file specified."
    use zipFile = new ZipFile(filePath)
    if zipFile.Count <> 2 || zipFile |> Seq.exists(fun x -> x.FileName <> "alpha" && x.FileName <> "beta")  then
        failwith "invalid file specified."
    zipFile |> fun x -> Parser.Parse(x.OpenReader())

I always expect that the selected file will be a valid zip archive containing 2 files without the extension: "alpha" and "beta".

Firstly, is there a better way to misinform my entrance?

My if statements are pretty long, and I'm sure F # can provide better solutions, but I really can't figure it out.

Secondly, using it failwithforces me to handle exceptions in my method MyForm.SelectFile(filePath:String), and I think options might be a better solution.

, , (ZipFile.IsZipFile ), ZipFile.

# null , , null , .

:

type Parser with

    static member isValidZipFile (zipFile:ZipFile) =
        (zipFile.Count = 2) && (zipFile |> Seq.forall(fun x -> (x.FileName = "alpha") || (x.FileName = "beta")))

    static member LoadFile(filePath:String) =
        if not <| ZipFile.IsZipFile(filePath) then
            None
        else
            use zipFile = new ZipFile(filePath)
            if not <| Parser.isValidZipFile(zipFile) then
                None
            else
                Some(seq { for zipEntry in zipFile do yield Parser.Parse(zipEntry.OpenReader()) } |> Seq.toArray)
+4
2

-, , :

zipFile.OpenReader() |> Parser.Parse

-, , Option. :

static member LoadFile(filePath:String) =
    if not <| ZipFile.IsZipFile(filePath) then None else
    use zipFile = new ZipFile(filePath)
    if zipFile.Count <> 2 || zipFile |> Seq.exists(fun x -> x.FileName <> "alpha" && x.FileName <> "beta") then None else
    Some (zipFile.OpenReader() |> Parser.Parse)

:

zipFile.OpenReader() |> Parser.Parse |> Some

, if . ! "" , .. isValidInput , isInvalidInput. , , zip :

let isValid (z:ZipFile) =
    z.Count = 2 && z |> Seq.forAll(fun x -> x.FileName = "alpha" || x.FileName = "beta")

LoadFile :

static member LoadFile(filePath:String) =
    if not <| ZipFile.IsZipFile(filePath) then None else
    use zipFile = new ZipFile(filePath)
    if not <| isValid zipFile then None else
    zipFile.OpenReader() |> Parser.Parse |> Some

, .

+4

. .

Some(seq { for zipEntry in zipFile do yield Parser.Parse(zipEntry.OpenReader()) } |> Seq.toArray)

,

zipFile |> Seq.map (fun ze -> ze.OpenReader () |> Parser.parse) |> Some

, (?)

zipFile |> Seq.map (fun ze -> ze.OpenReader () |> Parser.parse) |> Seq.toArray |> Some

option<seq<value>>. , , , .

+2

Source: https://habr.com/ru/post/1656816/


All Articles