From ca4248777b95fb74c0b8289a0a9bd46beac8f67b Mon Sep 17 00:00:00 2001 From: Rex Ng Date: Sun, 16 Jun 2024 19:38:29 +0800 Subject: [PATCH] Refactor --- DataParser.Console/FileRead.fs | 10 +++++----- DataParser.Console/Program.fs | 5 +---- DataParser.Console/ResultMap.fs | 3 +++ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/DataParser.Console/FileRead.fs b/DataParser.Console/FileRead.fs index 7dc8008..2ab1618 100644 --- a/DataParser.Console/FileRead.fs +++ b/DataParser.Console/FileRead.fs @@ -36,16 +36,16 @@ let readDataFiles folderPath = } let getDataFileFormats resultMap = - let folder (resultMap as ResultMap specs) state (filePath, fileName) = + let folder state (filePath, fileName) = match parseDataFileName fileName with | Error e -> Map.add filePath (Error e) state | Ok (DataFileName (formatName, _)) -> - let r = - match Map.tryFind formatName specs with + let dataFileFormatResult = + match ResultMap.tryFind formatName resultMap with | Some formatLines -> Result.bind (flip getDataFileFormat (filePath, fileName)) formatLines | None -> Error [FileFormatNotFound (ResultMap.keys resultMap, formatName)] - Map.add filePath r state + Map.add filePath dataFileFormatResult state - Seq.fold (folder resultMap) Map.empty + ResultMap << Seq.fold folder Map.empty \ No newline at end of file diff --git a/DataParser.Console/Program.fs b/DataParser.Console/Program.fs index b4f7887..9f2d8b1 100644 --- a/DataParser.Console/Program.fs +++ b/DataParser.Console/Program.fs @@ -23,10 +23,7 @@ printfn "Retrieving data files..." let dataFiles = readDataFiles dataFolderPath printfn "Parsing data files..." -let parsedDateFileFormats = - dataFiles - |> getDataFileFormats specs - |> ResultMap +let parsedDateFileFormats = getDataFileFormats specs dataFiles let dataFileParsedResults = ResultMap.bindResult parseDataFile parsedDateFileFormats diff --git a/DataParser.Console/ResultMap.fs b/DataParser.Console/ResultMap.fs index 9c09f19..cce63f7 100644 --- a/DataParser.Console/ResultMap.fs +++ b/DataParser.Console/ResultMap.fs @@ -16,6 +16,9 @@ module ResultMap = let keys (ResultMap x) = Set.ofSeq (Map.keys x) + let tryFind key (ResultMap m) = + Map.tryFind key m + let biIter f g (ResultMap x) = let go k = function Ok v -> f k v | Error e -> g k e Map.iter go x